• C# DragDropEffects类与拖动实现


    把文件或者目录直接拖放到你的程序上,这种效果用户体验不错。
    得到拖过来的路径的代码:(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 WinToXml
    {
        public partial class FrmMain : Form
        {
            public FrmMain()
            {
                InitializeComponent();
            }
            //Winform窗体文本框实现拖拽获得目录或文件路径(C#)

            protected void SetAllTextBox(Control org)
            {
                foreach (Control txt in org.Controls)
                {
                    if (txt is TextBox)
                    {
                        txt.AllowDrop = true;
                        txt.DragDrop += new DragEventHandler(txt_ObjDragDrop);
                        txt.DragEnter += new DragEventHandler(txt_ObjDragEnter);
                    }
                    else
                    {
                        if (txt.Controls.Count > 0)
                        {
                            SetAllTextBox(txt);
                        }
                    }
                }
            }

            private void txt_ObjDragEnter(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Link;//拖动时的图标
            }

            private void txt_ObjDragDrop(object sender, DragEventArgs e)
            {
                ((TextBox)sender).Text
                    = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            }

            private void FrmMain_Load(object sender, EventArgs e)
            {
                SetAllTextBox(this);
            }
        }
    }
    url:http://greatverve.cnblogs.com/archive/2012/03/03/DragDropEffects.html
    在应用程序中,是通过处理一系列事件,如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(3224); 
        this.listBox1.Name = "listBox1"
        this.listBox1.Size = new System.Drawing.Size(120280); 
        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(24824); 
        this.listBox2.Name = "listBox2"
        this.listBox2.Size = new System.Drawing.Size(120280); 
        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(614); 
        this.ClientSize = new System.Drawing.Size(408333); 
        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 

       /// <summary> 
       
    /// 应用程序的主入口点。 
       
    /// </summary> 
       [STAThread] 
       static void Main() 
       { 
        Application.Run(new Form1()); 
       } 

       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("Text")) 
        { 
         e.Effect = DragDropEffects.Move; 
        } 
       } 

       private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) 
       { 
        this.listBox2.Items.Add(e.Data.GetData("Text")); 
        this.listBox1.Items.Remove(e.Data.GetData("Text")); 
       } 

    }
    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所示。

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

    天祺围棋:www.tianqiweiqi.com呵呵

    凡事以大气象去面对,优秀是一种习惯。

  • 相关阅读:
    松软科技web课堂:SQLServer之UCASE() 函数
    松软科技web课堂:SQLServer之HAVING 子句
    SQLServer之GROUP BY语句
    松软科技web课堂:SQLServer之SUM() 函数
    松软科技web课堂:SQLServer之MIN() 函数
    SQLServer之MAX() 函数
    松软科技web课堂:SQLServer之LAST() 函数
    松软科技带你学开发:SQL--FIRST() 函数
    松软科技带你学开发:SQL--COUNT() 函数
    NodeJS初介
  • 原文地址:https://www.cnblogs.com/greatverve/p/DragDropEffects.html
Copyright © 2020-2023  润新知