项目中的一个树形结节,既要响应拖拽事件、又要响应点击事件。实现的时候没多想,依次实现了tree_MouseClick、tree_MouseDown、tree_MouseMove事件。出现的Bug是,偶尔会点击时不响应。

分析下来,应该是触发了MouseMove里的DoDragDrop拖拽事件,因此MouseClick被忽略了。如何正确实现呢?《Drap+Drop and MouseClick》这个帖子里给出了精确的解释,摘录如下:

Good implemented D'n'D doesn't start the operation on mouse down.
The drag operation has to start if the user presses the mouse button and
moves the mouse on a certain distance from the button-down point. Only if
this happens and all other conditions are met (see Bob's post) the
operation should be considered as starting D'n'D.

SystemInformation class has a property DragSize that should be used for
the size of the rectangle arround the click point in which the drag
operation shouldn't start.

大意就是说,不能一MouseMove就触发拖拽,而要超出一定距离之后,再触发。超出的范围,可以用系统设置SystemInformation.DragSize。于是在MouseMove里添加判断:

 private void tree_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDownPosition == Point.Empty ||
Math.Abs(e.X - mouseDownPosition.X) <= SystemInformation.DragSize.Width ||
Math.Abs(e.Y - mouseDownPosition.Y) <= SystemInformation.DragSize.Height)
{
return;
}
(sender as TreeList).DoDragDrop(data, DragDropEffects.Copy);
}

搞定。

Drag+Drop和MouseClick的更多相关文章

  1. HTML 学习笔记 (drag & drop)

    拖放(Drag & Drop)是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放.过去,我们用监听鼠标的Mousedown.Mouseo ...

  2. HTML5魔法堂:全面理解Drag & Drop API

    一.前言    在HTML4的时代,各前端工程师为了实现拖拽功能可说是煞费苦心,初听HTML5的DnD API觉得那些痛苦的日子将一去不复返,但事实又是怎样的呢?下面我们一起来看看DnD API的真面 ...

  3. Win10/UWP新特性—Drag&Drop 拖出元素到其他App

    在以前的文章中,写过微软新特性Drag&Drop,当时可能由于处于Win10预览版,使用的VS也是预览版,只实现了从桌面拖拽文件到UWP App中,没能实现从UWP拖拽元素到Desktop A ...

  4. Draggabilly – 轻松实现拖放功能(Drag & Drop)

    Draggabilly 是一个很小的 JavaScript 库,专注于拖放功能.只需要简单的设置参数就可以在你的网站用添加拖放功能.兼容 IE8+ 浏览器,支持多点触摸.可以灵活绑定事件,支持 Req ...

  5. JS魔法堂:IE5~9的Drag&Drop API

    一.前言     < HTML5魔法堂:全面理解Drag & Drop API>中提到从IE5开始已经支持DnD API,但IE5~9与HTML5的API有所不同,下面我们来了解一 ...

  6. 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop

    [源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...

  7. [转]人人网首页拖拽上传详解(HTML5 Drag&Drop、FileReader API、formdata)

    人人网首页拖拽上传详解(HTML5 Drag&Drop.FileReader API.formdata) 2011年12月11日 | 彬Go 上一篇:给力的 Google HTML5 训练营( ...

  8. Drag & Drop and File Reader

    参考 : http://www.html5rocks.com/zh/tutorials/file/dndfiles/ http://blog.csdn.net/rnzuozuo/article/det ...

  9. Android drag drop

    最近偶尔知道了锤子的one step,所以在网上看相关的东西,有人说android原生drag drop就能实现,我就去学习一下这个drag drop,下面把学习到的东西总结一下: drag drop ...

随机推荐

  1. Hashtable HashMap

    Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...

  2. HTML标签的默认样式

    body    有默认的内外边距(margin:0;padding:0); p         有默认的外边距(margin:0;)

  3. jQuery UI 实例 - 对话框(Dialog)(zhuan)

    http://www.runoob.com/jqueryui/example-dialog.html ************************************************* ...

  4. Java 如何得到 JVM 虚拟机的 System Properties

    Java 6 jps 命令得到进程号 jinfo -sysprops <PID> > sysprops.txt 打开 sysprops.txt 就可以查找 Language Time ...

  5. stdlib标准库的常用API

    iOS 有如下四种随机数方法,下面以产生 [0,100) 的随机数为例: 1. srand((unsigned)time(0));  //不加这句每次产生的随机数不变 int i = rand() % ...

  6. Convention插件 struts零配置

    http://blog.csdn.net/spyjava/article/details/13631961系列课程使用 注解:http://www.yiibai.com/struts_2/struts ...

  7. Android Studio新建Jni工程

    2.2版本的Android Studio支持新建Jni工程,不用再像以前自己构建工程目录,首先把自己的升级自己的AS到2.2以上 然后打开Tools->Andorid->SDK manag ...

  8. 小韦系统装工行网银U盾驱动的方法

    小韦系统装工行网银U盾驱动的方法 拷贝文件.bat @echo 开始注册echo n|copy /-y scarddlg.dll %windir%\system32\echo n|copy /-y w ...

  9. 例题:超市买东西的程序。输入商品信息,计算价格,价格满多少元打折。这道题用到结构体,集合,for循环,if else语句

    知识要点: 集合和数组的区别:数组是连续的,同一类型的一块区域,而集合可以是不连续的,多种数据类型的. 集合属性:.count 方法:.Add()  将对象添加到ArrayList中实际包含的元素数 ...

  10. 常用SQL语句(交互)

    %-------通配符 select * from [EMoney_Club].[dbo].[GoldIdea_AdviceCollect] where [Content] like '%昵称%' s ...