【功能】C# 控件实现内容拖动(DragDrop)功能(SamWang)

一、将控件内容拖到其他控件

  在开发过程中,经常会有客户要求,拖动一个控件的数据到另外一个控件中。例如将其中一个ListBox中的数据拖到另一个ListBox中。或者将DataGridView中的数据拖动到TreeView的某个节点。  

  在应用程序中,是通过处理一系列事件,如DragEnter,DragLeave和DragDrop事件来实现在Windows应用程序中的拖放操作的。通过使用这些事件参数中的可用信息,可以轻松实现拖放操作。 


  拖放操作在代码中是通过三步实现的,首先是启动拖放操作,在需要拖动数据的控件上实现MouseDown事件响应代码,并调用DoDragDrop()方法;其次是实现拖放效果,在目标控件上添加DragEnter事件响应代码,使用DragDropEffects枚举类型实现移动或复制等拖动效果;最后是放置数据操作,在目标控件上添加DragDrop响应代码,把数据添加到目标控件中。

  1 using System;
  2 using System.Drawing;
  3 using System.Collections;
  4 using System.ComponentModel;
  5 using System.Windows.Forms;
  6 using System.Data;
  7 
  8 namespace DragDrop
  9 {
 10     /// <summary> 
 11     /// Form1 的摘要说明。 
 12     /// </summary> 
 13     public class Form1 : System.Windows.Forms.Form
 14     {
 15         private System.Windows.Forms.ListBox listBox1;
 16         private System.Windows.Forms.ListBox listBox2;
 17         /// <summary> 
 18         /// 必需的设计器变量。 
 19         /// </summary> 
 20         private System.ComponentModel.Container components = null;
 21 
 22         public Form1()
 23         {
 24             // 
 25             // Windows 窗体设计器支持所必需的 
 26             // 
 27             InitializeComponent();
 28 
 29             // 
 30             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
 31             // 
 32         }
 33 
 34         /// <summary> 
 35         /// 清理所有正在使用的资源。 
 36         /// </summary> 
 37         protected override void Dispose(bool disposing)
 38         {
 39             if (disposing)
 40             {
 41                 if (components != null)
 42                 {
 43                     components.Dispose();
 44                 }
 45             }
 46             base.Dispose(disposing);
 47         }
 48 
 49         #region Windows 窗体设计器生成的代码
 50         /// <summary> 
 51         /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
 52         /// 此方法的内容。 
 53         /// </summary> 
 54         private void InitializeComponent()
 55         {
 56             this.listBox1 = new System.Windows.Forms.ListBox();
 57             this.listBox2 = new System.Windows.Forms.ListBox();
 58             this.SuspendLayout();
 59             // 
 60             // listBox1 
 61             // 
 62             this.listBox1.ItemHeight = 12;
 63             this.listBox1.Location = new System.Drawing.Point(32, 24);
 64             this.listBox1.Name = "listBox1";
 65             this.listBox1.Size = new System.Drawing.Size(120, 280);
 66             this.listBox1.TabIndex = 0;
 67             this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
 68             // 
 69             // listBox2 
 70             // 
 71             this.listBox2.ItemHeight = 12;
 72             this.listBox2.Location = new System.Drawing.Point(248, 24);
 73             this.listBox2.Name = "listBox2";
 74             this.listBox2.Size = new System.Drawing.Size(120, 280);
 75             this.listBox2.TabIndex = 0;
 76             this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
 77             this.listBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox2_DragEnter);
 78             // 
 79             // Form1 
 80             // 
 81             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
 82             this.ClientSize = new System.Drawing.Size(408, 333);
 83             this.Controls.Add(this.listBox1);
 84             this.Controls.Add(this.listBox2);
 85             this.Name = "Form1";
 86             this.Text = "Form1";
 87             this.Load += new System.EventHandler(this.Form1_Load);
 88             this.ResumeLayout(false);
 89 
 90         }
 91         #endregion
 92 
 93         private void Form1_Load(object sender, System.EventArgs e)
 94         {
 95             this.listBox1.AllowDrop = true;
 96             this.listBox2.AllowDrop = true;
 97             this.listBox1.Items.Add("a");
 98             this.listBox1.Items.Add("b");
 99             this.listBox1.Items.Add("c");
100         }
101 
102         private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
103         {
104             this.listBox1.DoDragDrop(this.listBox1.Items[this.listBox1.SelectedIndex], DragDropEffects.Move);
105         }
106 
107         private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
108         {
109             if (e.Data.GetDataPresent(DataFormats.Text))
110             {
111                 e.Effect = DragDropEffects.Move;
112             }
113         }
114 
115         private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
116         {
117             this.listBox2.Items.Add(e.Data.GetData(DataFormats.Text));
118             this.listBox1.Items.Remove(e.Data.GetData(DataFormats.Text));
119         }
120     }
121 }

 

 

二、将文件拖到控件中获得文件路径

  把文件或者目录直接拖放到你的程序上,这种效果用户体验不错。

  得到拖过来的路径的代码:(System.Array)e.Data.GetData(DataFormats.FileDrop)。

  然后你可以根据这些路径复制粘贴了。

  

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace TestFileDrag
11 {
12     public partial class Form1 : Form
13     {
14         public Form1()
15         {
16             InitializeComponent();
17         }
18 
19         private void Form1_Load(object sender, EventArgs e)
20         {
21             SetCtrlDrag.SetCtrlDragEvent(this.textBox1);
22         }
23     }
24 
25     public class SetCtrlDrag
26     {
27         public static void SetCtrlDragEvent(Control ctrl)
28         {
29             if(ctrl is TextBox)
30             {
31                 TextBox tb = ctrl as TextBox;
32                 tb.AllowDrop = true;
33                 tb.DragEnter += (sender, e) =>
34                 {
35                     e.Effect = DragDropEffects.Link;//拖动时的图标
36                 };
37                 tb.DragDrop += (sender, e) =>
38                 {
39                     ((TextBox)sender).Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
40                 };
41             }
42         }
43     }
44 }

界面效果

TestFilePathDrag

三、相关说明

msdn:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.dragdropeffects.aspx

1.方法

  实现拖放效果时,C#中提供了一个系统方法DoDragDrop方法,用于实现开始拖放操作,该方法由Control类所定义,由于控件均直接或是间接派生于Control类,因此开发人员可以在任何可视化组件中调用DoDragDrop方法。DoDragDrop方法使用语法如下:

  public DragDropEffects DoDragDrop ( Object data,DragDropEffects allowedEffects)

  data:用户所要拖动的数据内容。必须将所要拖动的内容传入到这个方法的第一个参数位置。

  allowedEffects:DragDropEffects枚举值之一,此类型包含了拖动操作的效果。DragDropEffects枚举值如表32.8所示。

  DragDropEffects枚举值:
    枚举值 说明 
    All 从拖动源复制、移除数据,并将其滚动到放置目标中 
    Copy 将数据复制到放置目标 
    Link 将拖动源中的数据链接到放置目标 
    Move 将拖动源的数据移动到放置目标 
    None 放置目标不接受该数据 
    Scroll 即将在放置目标中开始滚动,或当前正在滚动


  开发人员在使用DoDragDrop方法时,必须指定参数allowedEffects为表中的任何一个成员,另外,还可以使用位运算符,把其中的任何一个成员作为一个完整参数传入,以得到所需的拖动效果,实现关键代码如下:

    DragDropEffects.Copy| DragDropEffects.None

 

2.事件

  C#中提供了一个系统拖放事件,与拖放方法一起使用来达到更好的效果。常用的拖放事件如表所示。

  表  拖放事件

    名称 说明 
    DragEnter 当用户在拖放操作过程中首次将鼠标光标拖到控件上时,会引发该事件 
    DragDrop 在完成拖放操作时发生 
    GiveFeedback 在执行拖动操作期间发生 
    DragLeave 如果用户移出一个窗口,则引发DragLeave事件 
    DragOver 如果鼠标移动但停留在同一个控件中,则引发DragOver事件 
    QueryContinueDrag 在拖放操作过程中,当键盘或鼠标按钮状态发生变化时,会引发QueryContinueDrag 事件。QueryContinueDrag事件允许拖动源确定是否应取消拖放操作

 

 


作者:SamWang
出处:http://wangshenhe.cnblogs.com/
本文版权归作者和博客园共有,欢迎围观转载。转载时请您务必在文章明显位置给出原文链接,谢谢您的合作。

 

原文地址:https://www.cnblogs.com/wangshenhe/p/2490193.html