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应用程序中的拖放操作的。通过使用这些事件参数中的可用信息,可以轻松实现拖放操作。 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值