zoukankan      html  css  js  c++  java
  • 在C#中快速实现拖放操作

    拖放操作是一个我比较喜欢的用户体验,但实现起来稍显麻烦,这里我将它的常用方式简单的集合了一下,作为扩展方法,以便快速调用:

    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来实现要简洁得多。

  • 相关阅读:
    喜欢的诗
    诗集与集诗
    oracle 12c 中asm元数据是否有所变化
    hdu2066一个人的旅行(dijkstra)
    单链表
    ExtJS4.2学习(7)——基础知识之Reader&Writer篇
    hdu3790最短路径问题 (用优先队列实现的)
    poj 1220 NUMBER BASE CONVERSION(短除法进制转换)
    POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)
    全排列
  • 原文地址:https://www.cnblogs.com/TianFang/p/1294984.html
Copyright © 2011-2022 走看看