拖放操作是一个我比较喜欢的用户体验,但实现起来稍显麻烦,这里我将它的常用方式简单的集合了一下,作为扩展方法,以便快速调用: static class DrapDropExtend {
拖放操作是一个我比较喜欢的用户体验,但实现起来稍显麻烦,这里我将它的常用方式简单的集合了一下,作为扩展方法,以便快速调用:
static class DrapDropExtend
{
public static void SimpleDrapDrop<T>(this Control c, string datafORMat, Action<T> hanlder) where T : class
{
c.AllowDrop = true;
c.DragEnter += (s, e) =>
{
if (e.Data.GetDataPresent(dataformat))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
};
c.DragDrop += (s, e) =>
{
var data = e.Data.GetData(dataformat) as T;
hanlder(data);
};
}
public static void SimpleDrapDrop(this Control c, Action<DragEventArgs> enterHanlder, Action<DragEventArgs> dropHanlder)
{
c.AllowDrop = true;
c.DragEnter += (s, e) => enterHanlder(e);
c.DragDrop += (s, e) => enterHanlder(e);
}
public static void SimpleDrapDrop(this Control c, DragEventHandler enterHanlder, DragEventHandler dropHanlder)
{
c.AllowDrop = true;
c.DragEnter += enterHanlder;
c.DragDrop += dropHanlder;
}
}
该类使得实现拖放更加简单了,一个简单的示例如下:
public Form1()
{
InitializeComponent();
this.SimpleDrapDrop<string>(DataFormats.Text, x => this.Text = x);
}
这比通过IDE来实现要简洁得多。
--结束END--
本文标题: C#快速实现拖放操作
本文链接: https://www.lsjlt.com/news/151666.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0