利用TextBox简单的实现DragEnter、DragDrop拖放效果
不废话直接上代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test.Controls
{
public class TextBoxEx:TextBox
{
public TextBoxEx()
{
base.AllowDrop = true;
this.DragDrop += TextBoxEx2_DragDrop;
this.DragEnter += TextBoxEx2_DragEnter;
}
//DragDrop DragEnter 事件要共同使用
private void TextBoxEx2_DragDrop(object sender, DragEventArgs e)
{
//拖放完成时
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] paths = (string[])e.Data.GetData(DataFormats.FileDrop);
this.Text = paths[0];//取得文件名// paths[0] 为第1个文件全路径
}
}
private void TextBoxEx2_DragEnter(object sender, DragEventArgs e)
{
//拖放进入时
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}