博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Drag+Drop和MouseClick
阅读量:4449 次
发布时间:2019-06-07

本文共 1233 字,大约阅读时间需要 4 分钟。

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

分析下来,应该是触发了MouseMove里的DoDragDrop拖拽事件,因此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里添加判断:

1 private void tree_MouseMove(object sender, MouseEventArgs e) 2 { 3     if (mouseDownPosition == Point.Empty || 4         Math.Abs(e.X - mouseDownPosition.X) <= SystemInformation.DragSize.Width || 5         Math.Abs(e.Y - mouseDownPosition.Y) <= SystemInformation.DragSize.Height) 6     { 7         return; 8     } 9     (sender as TreeList).DoDragDrop(data, DragDropEffects.Copy);10 }

搞定。

转载于:https://www.cnblogs.com/AlexanderYao/p/3765927.html

你可能感兴趣的文章
[myeclipse]@override报错问题
查看>>
자주 쓰이는 정규표현식
查看>>
超简单的listview单选模式SingleMode(自定义listview item)
查看>>
vue-11-路由嵌套-参数传递-路由高亮
查看>>
HDU 1199 - Color the Ball 离散化
查看>>
[SCOI2005]骑士精神
查看>>
Hibernate原理解析-Hibernate中实体的状态
查看>>
六时车主 App 隐私政策
查看>>
C语言常见问题 如何用Visual Studio编写C语言程序测试
查看>>
Web用户的身份验证及WebApi权限验证流程的设计和实现
查看>>
hdu 2098 分拆素数和
查看>>
[ONTAK2010]Peaks kruskal重构树,主席树
查看>>
ECMAScript6-let与const命令详解
查看>>
iOS 使用系统相机、相册显示中文
查看>>
什么是敏捷设计
查看>>
SCSS的基本操作
查看>>
"安装程序无法定位现有系统分区" 问题解决
查看>>
.NET中栈和堆的比较
查看>>
【莫队】bzoj 3781,bzoj 2038,bzoj 3289
查看>>
如何优化limit
查看>>