首页  ·  知识 ·  编程语言
C#控件实现内容拖动(DragDrop)功能
网友    .NET  编辑:dezai   图片来源:网络
在开发过程中,经常会有客户要求,拖动一个控件的数据到另外一个控件中。例如将其中一个ListBox中的数据拖到另一个ListBox中。或者将DataGridView中的数据拖动到
 

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

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

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


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

 

using System;
 using System.Drawing;
 using System.Collections;
 using System.ComponentModel;
 using System.Windows.Forms;
 using System.Data;
 
 namespace DragDrop
 {
     /// <summary>
     /// Form1 的摘要说明。
     /// </summary>
     public class Form1 : System.Windows.Forms.Form
     {
         private System.Windows.Forms.ListBox listBox1;
         private System.Windows.Forms.ListBox listBox2;
         /// <summary>
         /// 必需的设计器变量。
         /// </summary>
         private System.ComponentModel.Container components = null;
 
         public Form1()
         {
             //
             // Windows 窗体设计器支持所必需的
             //
             InitializeComponent();
 
             //
             // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
             //
         }
 
         /// <summary>
         /// 清理所有正在使用的资源。
         /// </summary>
         protected override void Dispose(bool disposing)
         {
             if (disposing)
             {
                 if (components != null)
                 {
                     components.Dispose();
                 }
             }
             base.Dispose(disposing);
         }
 
         #region Windows 窗体设计器生成的代码
         /// <summary>
         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
         /// 此方法的内容。
         /// </summary>
         private void InitializeComponent()
         {
             this.listBox1 = new System.Windows.Forms.ListBox();
             this.listBox2 = new System.Windows.Forms.ListBox();
             this.SuspendLayout();
             //
             // listBox1
             //
             this.listBox1.ItemHeight = 12;
             this.listBox1.Location = new System.Drawing.Point(32, 24);
             this.listBox1.Name = "listBox1";
             this.listBox1.Size = new System.Drawing.Size(120, 280);
             this.listBox1.TabIndex = 0;
             this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
             //
             // listBox2
             //
             this.listBox2.ItemHeight = 12;
             this.listBox2.Location = new System.Drawing.Point(248, 24);
             this.listBox2.Name = "listBox2";
             this.listBox2.Size = new System.Drawing.Size(120, 280);
             this.listBox2.TabIndex = 0;
             this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
             this.listBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox2_DragEnter);
             //
             // Form1
             //
             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
             this.ClientSize = new System.Drawing.Size(408, 333);
             this.Controls.Add(this.listBox1);
             this.Controls.Add(this.listBox2);
             this.Name = "Form1";
             this.Text = "Form1";
             this.Load += new System.EventHandler(this.Form1_Load);
             this.ResumeLayout(false);
 
         }
         #endregion
 
         private void Form1_Load(object sender, System.EventArgs e)
         {
             this.listBox1.AllowDrop = true;
             this.listBox2.AllowDrop = true;
             this.listBox1.Items.Add("a");
             this.listBox1.Items.Add("b");
             this.listBox1.Items.Add("c");
         }
 
         private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
         {
             this.listBox1.DoDragDrop(this.listBox1.Items[this.listBox1.SelectedIndex], DragDropEffects.Move);
         }
 
         private void listBox2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
         {
             if (e.Data.GetDataPresent(DataFormats.Text))
             {
                 e.Effect = DragDropEffects.Move;
             }
         }
 
         private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
         {
             this.listBox2.Items.Add(e.Data.GetData(DataFormats.Text));
             this.listBox1.Items.Remove(e.Data.GetData(DataFormats.Text));
         }
     }
 }

 

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

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

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

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

 
using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 
 namespace TestFileDrag
 {
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
 
         private void Form1_Load(object sender, EventArgs e)
         {
             SetCtrlDrag.SetCtrlDragEvent(this.textBox1);
         }
     }
 
     public class SetCtrlDrag
     {
         public static void SetCtrlDragEvent(Control ctrl)
         {
             if(ctrl is TextBox)
             {
                 TextBox tb = ctrl as TextBox;
                 tb.AllowDrop = true;
                 tb.DragEnter += (sender, e) =>
                 {
                     e.Effect = DragDropEffects.Link;//拖动时的图标
                 };
                 tb.DragDrop += (sender, e) =>
                 {
                     ((TextBox)sender).Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                 };
             }
         }
     }
 }
界面效果
 

三、相关说明

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事件允许拖动源确定是否应取消拖放操作

本文作者:网友 来源:网络
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读