WinForm拖拽控件生成副本的解決方法
本文講述了WinForm中實(shí)現(xiàn)拖拽效果的功能,即在WinForm中有一個(gè)Button,可以實(shí)現(xiàn)拖拽這個(gè)Button到目標(biāo)位置后生成一個(gè)該控件的副本的功能。具體操作步驟如下:
要實(shí)現(xiàn)該功能主要分成如下三步:
1)確定被拖拽的對(duì)象:這里是Button(要使得Button被單擊之后可以拖拽,那么必須處理其MouseDown事件,同時(shí)調(diào)用其DoDragDrop——該函數(shù)接受兩個(gè)參數(shù):i)要拖動(dòng)的數(shù)據(jù)。ii)拖動(dòng)的效果(該效果是2“目標(biāo)位置”所能夠接受的效果,是一個(gè)枚舉值):
C#代碼如下:
Button1.DoDragDrop(Button1, DragDropEffects.Copy || DragDropEffects.Move); //形成拖拽效果,移動(dòng)+拷貝的組合效果
VB.NET頁(yè)面代碼如下:
Button1.DoDragDrop(Button1, DragDropEffects.Copy Or DragDropEffects.Move) '形成拖拽效果,移動(dòng)+拷貝的組合效果
2)目標(biāo)位置:這里是Form窗體自身。為了使得和Windows資源管理器中實(shí)現(xiàn)的文件拖拽效果一樣(即拖拽一個(gè)文件到目標(biāo)位置的中途,鼠標(biāo)出現(xiàn)“+”號(hào)的效果)。那么應(yīng)當(dāng)處理DragEnter事件——即拖拽控件途中進(jìn)入Form體內(nèi)把效果設(shè)置成Copy的效果。
C#代碼如下:
private void Form1_DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e) { //當(dāng)Button被拖拽到WinForm上時(shí)候,鼠標(biāo)效果出現(xiàn) if ((e.Data.GetDataPresent(typeof(Button)))) { e.Effect = DragDropEffects.Copy; } }
VB.NET頁(yè)面代碼如下:
Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter If (e.Data.GetDataPresent(GetType(Button))) Then '當(dāng)Button被拖拽到WinForm上時(shí)候,鼠標(biāo)效果出現(xiàn) e.Effect = DragDropEffects.Copy End If End Sub
同時(shí),為了使得Form自身支持接受拖拽傳來(lái)的控件,必須設(shè)置其AllowDrag=True:
另外,一旦松開(kāi)鼠標(biāo),那么拖拽過(guò)程結(jié)束。此時(shí)應(yīng)當(dāng)處理DragDrop事件,復(fù)制一個(gè)按鈕:
C#代碼如下:
private void Form1_DragDrop(System.Object sender, System.Windows.Forms.DragEventArgs e) { //拖放完畢之后,自動(dòng)生成新控件 Button btn = new Button(); btn.Size = Button1.Size; btn.Location = this.PointToClient(new Point(e.X, e.Y)); //用這個(gè)方法計(jì)算出客戶端容器界面的X,Y坐標(biāo)。否則直接使用X,Y是屏幕坐標(biāo) this.Controls.Add(btn); btn.Text = "按鈕" + count.ToString; count = count + 1; }
VB.NET頁(yè)面代碼如下:
Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop '拖放完畢之后,自動(dòng)生成新控件 Dim btn As New Button btn.Size = Button1.Size btn.Location = Me.PointToClient(New Point(e.X, e.Y)) '用這個(gè)方法計(jì)算出客戶端容器界面的X,Y坐標(biāo)。否則直接使用X,Y是屏幕坐標(biāo) Me.Controls.Add(btn) btn.Text = "按鈕" + count.ToString count = count + 1 End Sub
這里需要注意點(diǎn):Location屬性(指定控件放置位置的起始點(diǎn))不能直接用e.X或e.Y——因?yàn)檫@是屏幕坐標(biāo),要根據(jù)實(shí)際的控件界面坐標(biāo)進(jìn)行適度轉(zhuǎn)換,最簡(jiǎn)單方法是——PointToClient方法。
下面給出完整代碼:
【界面如下所示】
C#代碼如下:
using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; public class Form1 { //計(jì)數(shù)變量,說(shuō)明輸出了第N個(gè)Button private int count = 1; private void Form1_Load(System.Object sender, System.EventArgs e) { this.AllowDrop = true; //窗體自身支持接受拖拽來(lái)的控件 } private void Button1_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e) { //左鍵的話,標(biāo)志位為true(表示拖拽開(kāi)始) if ((e.Button == System.Windows.Forms.MouseButtons.Left)) { Button1.DoDragDrop(Button1, DragDropEffects.Copy | DragDropEffects.Move); //形成拖拽效果,移動(dòng)+拷貝的組合效果 } } private void Form1_DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e) { //當(dāng)Button被拖拽到WinForm上時(shí)候,鼠標(biāo)效果出現(xiàn) if ((e.Data.GetDataPresent(typeof(Button)))) { e.Effect = DragDropEffects.Copy; } } private void Form1_DragDrop(System.Object sender, System.Windows.Forms.DragEventArgs e) { //拖放完畢之后,自動(dòng)生成新控件 Button btn = new Button(); btn.Size = Button1.Size; btn.Location = this.PointToClient(new Point(e.X, e.Y)); //用這個(gè)方法計(jì)算出客戶端容器界面的X,Y坐標(biāo)。否則直接使用X,Y是屏幕坐標(biāo) this.Controls.Add(btn); btn.Text = "按鈕" + count.ToString(); count = count + 1; } public Form1() { DragDrop += Form1_DragDrop; DragEnter += Form1_DragEnter; Load += Form1_Load; } }
VB.NET頁(yè)面代碼如下:
Public Class Form1 '計(jì)數(shù)變量,說(shuō)明輸出了第N個(gè)Button Private count As Integer = 1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.AllowDrop = True '窗體自身支持接受拖拽來(lái)的控件 End Sub Private Sub Button1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown '左鍵的話,標(biāo)志位為true(表示拖拽開(kāi)始) If (e.Button = Windows.Forms.MouseButtons.Left) Then Button1.DoDragDrop(Button1, DragDropEffects.Copy Or DragDropEffects.Move) '形成拖拽效果,移動(dòng)+拷貝的組合效果 End If End Sub Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter If (e.Data.GetDataPresent(GetType(Button))) Then '當(dāng)Button被拖拽到WinForm上時(shí)候,鼠標(biāo)效果出現(xiàn) e.Effect = DragDropEffects.Copy End If End Sub Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop '拖放完畢之后,自動(dòng)生成新控件 Dim btn As New Button btn.Size = Button1.Size btn.Location = Me.PointToClient(New Point(e.X, e.Y)) '用這個(gè)方法計(jì)算出客戶端容器界面的X,Y坐標(biāo)。否則直接使用X,Y是屏幕坐標(biāo) Me.Controls.Add(btn) btn.Text = "按鈕" + count.ToString count = count + 1 End Sub End Class
相關(guān)文章
C# 并行和多線程編程——認(rèn)識(shí)和使用Task
這篇文章主要介紹了C# 并行和多線程編程——認(rèn)識(shí)和使用Task的的相關(guān)資料,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法
這篇文章主要介紹了C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法,涉及C#數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11C#寫入對(duì)象或集合類型數(shù)據(jù)到xml文件的方法
這篇文章主要介紹了C#寫入對(duì)象或集合類型數(shù)據(jù)到xml文件的方法,涉及C#針對(duì)XML文件的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07c# 使用Entity Framework操作Access數(shù)據(jù)庫(kù)的示例
本篇文章主要介紹了c# 使用Entity Framework操作Access數(shù)據(jù)庫(kù)的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Unity?UGUI的RawImage原始圖片組件使用示例詳解
這篇文章主要為大家介紹了Unity?UGUI的RawImage原始圖片組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07C# 中如何利用lambda實(shí)現(xiàn)委托事件的掛接
在寫一個(gè)小程序的時(shí)候,碰到了這樣的問(wèn)題,需要用委托來(lái)掛接事件,但是又想在這事件中使用局部的變量,而委托一旦定義好后,掛接方就沒(méi)有辦法再添加額外的形參了。那有沒(méi)有什么辦法,可以實(shí)現(xiàn)呢2013-07-07