• C#拖拽操作


    C#的拖拽

    本文将以Winform为例

    有两个主要的事件:

    DragEnter 拖拽到区域中触发的事件

    DragDrop 当拖拽落下的时候出发此事件

    饮水思源

    参考博客: http://www.cnblogs.com/gossip/archive/2010/02/22/1671126.html

    Demo 模拟拖拽上传

    我们在开发中有时候回用到拖拽上传的功能,例如QQ邮箱上传附件的时候,博客园Mark Down编辑器上传图片的时候。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace StudyDrag
    {
        public partial class frmDragUpload : Form
        {
            public frmDragUpload()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 初始化属性
            /// </summary>
            void Init()
            {
                //设置为允许拖拽
                this.AllowDrop = true;
            }
    
            /// <summary>
            /// 注册事件
            /// </summary>
            void RegisteEvent()
            {
                this.DragEnter += FrmDragUpload_DragEnter;
                this.DragDrop += FrmDragUpload_DragDrop;
            }
    
            /// <summary>
            /// 当在此区域拖动落下的时候出发
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void FrmDragUpload_DragDrop(object sender, DragEventArgs e)
            {
                //获取拖动的数据
                IDataObject ido = e.Data;
                //如果拖动的数据是文件类型
                if (ido.GetDataPresent(DataFormats.FileDrop))
                {
                    //获取文件的路径
                    string[] paths = (string[])ido.GetData(DataFormats.FileDrop);
    
                    //执行我们想要执行的业务逻辑,读取上传到服务器等等。
                    foreach (var item in paths)
                    {
                        Console.WriteLine(item);
                    }
                    MessageBox.Show("上传成功!");
                }
                else
                {
                    MessageBox.Show("上传类型错误!");
                }
            }
    
            /// <summary>
            /// 当拖动到此区域的时候触发
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void FrmDragUpload_DragEnter(object sender, DragEventArgs e)
            {
                //设置拖动的影响为:从拖动源复制到拖动的目标
                e.Effect = DragDropEffects.Copy;
            }
    
            /// <summary>
            /// 窗体初始化
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void frmDragUpload_Load(object sender, EventArgs e)
            {
                this.Init();
                this.RegisteEvent();
            }
        }
    }
    
    

    Demo 2 ListView拖拽到TreeView上

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace StudyDrag
    {
        public partial class frmDragListView : Form
        {
            public frmDragListView()
            {
                InitializeComponent();
            }
    
            /// <summary>
            /// 初始化属性
            /// </summary>
            void Init()
            {
                this.lbDep.AllowDrop = true;
                this.tvDep.AllowDrop = true;
            }
    
            /// <summary>
            /// 加载一些数据
            /// </summary>
            void LoadData()
            {
                for (int i = 1; i <= 10; i++)
                {
                    string itemValue = "有关部门" + i.ToString();
                    this.lbDep.Items.Add(itemValue);
                }
            }
    
            void RegisterEvent()
            {
                this.tvDep.DragDrop += TvDep_DragDrop;
                this.tvDep.DragEnter += TvDep_DragEnter;
            }
    
            /// <summary>
            /// 当拖拽进入到该区域的时候出发的事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void TvDep_DragEnter(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Copy;
            }
    
            /// <summary>
            /// 当完成退拽时触发的事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void TvDep_DragDrop(object sender, DragEventArgs e)
            {
                string item = (string)e.Data.GetData(DataFormats.Text);
                this.tvDep.Nodes.Add(item);
            }
    
            /// <summary>
            /// 窗体初始化
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void frmDragListView_Load(object sender, EventArgs e)
            {
                Init();
                LoadData();
                RegisterEvent();
            }
    
            /// <summary>
            /// 鼠标点击的的时候开始执行拖拽操作
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void lbDep_MouseDown(object sender, MouseEventArgs e)
            {
                if (lbDep.SelectedItem == null)
                {
                    return;
                }
    			
                lbDep.DoDragDrop(lbDep.SelectedItem, DragDropEffects.Copy);
            }
        }
    }
    
    

    源码下载

    我的码云:https://git.oschina.net/ShareKnowledge/drag

  • 相关阅读:
    页面监控容器实现记录
    负载均衡基础理论
    asp.net部署时加密config文件
    还原bak到localdb的问题:The logical database file cannot be found ldf
    Could not load file or assembly 'System.Data.SQLite ... 试图加载格式不正确的程序
    Window vista 以上制作自定义证书并为端口配置ssl
    1-6、算法设计常用思想之迭代法
    1-5、算法设计常用思想之穷举法
    1-4、算法设计常用思想之动态规划法
    游戏开发-cocos creator踩坑-bind(this)导致的事件监听off不掉
  • 原文地址:https://www.cnblogs.com/slyfox/p/7116647.html
Copyright © 2020-2023  润新知