WinForm拖拽控件生成正本的處理辦法。本站提示廣大學習愛好者:(WinForm拖拽控件生成正本的處理辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是WinForm拖拽控件生成正本的處理辦法正文
本文講述了WinForm中完成拖拽後果的功效,即在WinForm中有一個Button,可以完成拖拽這個Button到目的地位後生成一個該控件的正本的功效。詳細操作步調以下:
要完成該功效重要分紅以下三步:
1)肯定被拖拽的對象:這裡是Button(要使得Button被單擊以後可以拖拽,那末必需處置其MouseDown事宜,同時挪用其DoDragDrop——該函數接收兩個參數:i)要拖動的數據。ii)拖動的後果(該後果是2“目的地位”所可以或許接收的後果,是一個列舉值):
C#代碼以下:
Button1.DoDragDrop(Button1, DragDropEffects.Copy || DragDropEffects.Move); //構成拖拽後果,挪動+拷貝的組合後果
VB.NET頁面代碼以下:
Button1.DoDragDrop(Button1, DragDropEffects.Copy Or DragDropEffects.Move) '構成拖拽後果,挪動+拷貝的組合後果
2)目的地位:這裡是Form窗體本身。為了使得和Windows資本治理器中完成的文件拖拽後果一樣(即拖拽一個文件到目的地位的半途,鼠標湧現“+”號的後果)。那末應該處置DragEnter事宜——即拖拽控件途中進入Form體內把後果設置成Copy的後果。
C#代碼以下:
private void Form1_DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e)
{
//當Button被拖拽到WinForm上時刻,鼠標後果湧現
if ((e.Data.GetDataPresent(typeof(Button)))) {
e.Effect = DragDropEffects.Copy;
}
}
VB.NET頁面代碼以下:
Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
If (e.Data.GetDataPresent(GetType(Button))) Then '當Button被拖拽到WinForm上時刻,鼠標後果湧現
e.Effect = DragDropEffects.Copy
End If
End Sub
同時,為了使得Form本身支撐接收拖拽傳來的控件,必需設置其AllowDrag=True:
別的,一旦松開鼠標,那末拖拽進程停止。此時應該處置DragDrop事宜,復制一個按鈕:
C#代碼以下:
private void Form1_DragDrop(System.Object sender, System.Windows.Forms.DragEventArgs e)
{
//拖放終了以後,主動生成新控件
Button btn = new Button();
btn.Size = Button1.Size;
btn.Location = this.PointToClient(new Point(e.X, e.Y));
//用這個辦法盤算出客戶端容器界面的X,Y坐標。不然直接應用X,Y是屏幕坐標
this.Controls.Add(btn);
btn.Text = "按鈕" + count.ToString;
count = count + 1;
}
VB.NET頁面代碼以下:
Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
'拖放終了以後,主動生成新控件
Dim btn As New Button
btn.Size = Button1.Size
btn.Location = Me.PointToClient(New Point(e.X, e.Y)) '用這個辦法盤算出客戶端容器界面的X,Y坐標。不然直接應用X,Y是屏幕坐標
Me.Controls.Add(btn)
btn.Text = "按鈕" + count.ToString
count = count + 1
End Sub
這裡須要留意點:Location屬性(指定控件放置地位的肇端點)不克不及直接用e.X或e.Y——由於這是屏幕坐標,要依據現實的控件界面坐標停止過度轉換,最簡略辦法是——PointToClient辦法。
上面給出完全代碼:
【界面以下所示】

C#代碼以下:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
//計數變量,解釋輸入了第N個Button
private int count = 1;
private void Form1_Load(System.Object sender, System.EventArgs e)
{
this.AllowDrop = true;
//窗體本身支撐接收拖拽來的控件
}
private void Button1_MouseDown(System.Object sender, System.Windows.Forms.MouseEventArgs e)
{
//左鍵的話,標記位為true(表現拖拽開端)
if ((e.Button == System.Windows.Forms.MouseButtons.Left)) {
Button1.DoDragDrop(Button1, DragDropEffects.Copy | DragDropEffects.Move);
//構成拖拽後果,挪動+拷貝的組合後果
}
}
private void Form1_DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e)
{
//當Button被拖拽到WinForm上時刻,鼠標後果湧現
if ((e.Data.GetDataPresent(typeof(Button)))) {
e.Effect = DragDropEffects.Copy;
}
}
private void Form1_DragDrop(System.Object sender, System.Windows.Forms.DragEventArgs e)
{
//拖放終了以後,主動生成新控件
Button btn = new Button();
btn.Size = Button1.Size;
btn.Location = this.PointToClient(new Point(e.X, e.Y));
//用這個辦法盤算出客戶端容器界面的X,Y坐標。不然直接應用X,Y是屏幕坐標
this.Controls.Add(btn);
btn.Text = "按鈕" + count.ToString();
count = count + 1;
}
public Form1()
{
DragDrop += Form1_DragDrop;
DragEnter += Form1_DragEnter;
Load += Form1_Load;
}
}
VB.NET頁面代碼以下:
Public Class Form1
'計數變量,解釋輸入了第N個Button
Private count As Integer = 1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True '窗體本身支撐接收拖拽來的控件
End Sub
Private Sub Button1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
'左鍵的話,標記位為true(表現拖拽開端)
If (e.Button = Windows.Forms.MouseButtons.Left) Then
Button1.DoDragDrop(Button1, DragDropEffects.Copy Or DragDropEffects.Move) '構成拖拽後果,挪動+拷貝的組合後果
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 '當Button被拖拽到WinForm上時刻,鼠標後果湧現
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
'拖放終了以後,主動生成新控件
Dim btn As New Button
btn.Size = Button1.Size
btn.Location = Me.PointToClient(New Point(e.X, e.Y)) '用這個辦法盤算出客戶端容器界面的X,Y坐標。不然直接應用X,Y是屏幕坐標
Me.Controls.Add(btn)
btn.Text = "按鈕" + count.ToString
count = count + 1
End Sub
End Class