简体   繁体   English

如何区分.Net中的单击和双击?

[英]How can I differentiate between single and double clicks in .Net?

I want to override OnMouseClick and OnMouseDoubleClick and perform different actions depending on which click style was used. 我想覆盖OnMouseClick和OnMouseDoubleClick,并根据使用的单击样式执行不同的操作。

The problem is that OnMouseClick is happening for both single and double clicks, and is getting called before OnMouseDoubleClick. 问题是OnMouseClick正在发生单击和双击,并且在OnMouseDoubleClick之前被调用。

I'm certain this must be a common requirement, so I guess I'm missing something pretty obvious. 我确信这一定是一个常见的要求,所以我想我错过了一些非常明显的东西。 Can someone fill me in? 有人能填补我吗?

Edit to add: the MouseEventArgs.Clicks count doesn't help. 编辑添加:MouseEventArgs.Clicks计数没有帮助。 In the case of a double-click, the first click is handled as a single click in OnMouseClick with MouseEventArgs.Clicks == 1. 如果是双击,则第一次单击将在OnMouseClick中单击一次,并使用MouseEventArgs.Clicks == 1。

Edit to add: the objects are image thumbnails. 编辑添加:对象是图像缩略图。 Single click should toggle selection on and off for export. 单击即可打开和关闭选择以进行导出。 Double click should make the thumbnail full screen. 双击应使缩略图全屏。 The selection and "activation" actions are orthogonal. 选择和“激活”动作是正交的。 This may indicate an underlying problem with the two actions... 这可能表明这两个行动存在潜在问题......

Cheers, Rob 干杯,罗布

That happens throughout Windows. 这种情况发生在整个Windows I don't think they added anything special to handle it in .Net. 我不认为他们在.Net中添加任何特殊处理它。

The normal means of handling this is 处理这个问题的正常方法是

  • a) just make single click something you'd want to happen before a double click, like select. a)在双击之前单击一下你想要发生的事情,比如选择。
  • b) if that's not an option, then on the click event, start a timer. b)如果那不是一个选项,那么在点击事件上,启动一个计时器。 On the timer tick, do the single click action. 在计时器刻度上,执行单击操作。 If the double-click event occurs first, kill the timer, and do the double click action. 如果首先发生双击事件,请终止计时器,然后执行双击操作。

The amount of time you set the time for should be equal to the system's Double-Click time (which the user can specify in the control panel). 您设置时间的时间应该等于系统的双击时间(用户可以在控制面板中指定)。 It's available from System.Windows.Forms.SystemInformation.DoubleClickTime. 它可以从System.Windows.Forms.SystemInformation.DoubleClickTime获得。 The full details are in the MSDN, here 完整的详细信息在MSDN中, 这里

The issue is that most objects don't implement both. 问题是大多数对象都没有实现这两个目标。 The few objects that can reasonably have different actions for single and double clicks generally are OK to run the single click action before the double click action (highlight then open a folder, enter focus on an address bar before selecting it, etc.) The only way to determine would probably be to wait on the single click for an amount of time and cancel the action for the single click if a double click is detected. 对于单击和双击可以合理地具有不同操作的少数对象通常可以在双击操作之前运行单击操作(突出显示然后打开文件夹,在选择之前在地址栏上输入焦点等)。确定的方法可能是等待单击一段时间,如果检测到双击,则取消单击的操作。

Another possible solution is to have the OnMouseDoubleClick function call OnMouseClick. 另一种可能的解决方案是让OnMouseDoubleClick函数调用OnMouseClick。 Since the action in OnMouseClick is a binary toggle, calling it twice resets it to the same state. 由于OnMouseClick中的操作是二进制切换,因此调用它两次会将其重置为相同的状态。

So when the user double clicks, windows calls OnMouseClick, then OnMouseDoubleClick. 因此,当用户双击时,Windows会调用OnMouseClick,然后调用OnMouseDoubleClick。 OnMouseDoubleClick calls OnMouseClick (again) to restore the state, then handles the double click. OnMouseDoubleClick调用OnMouseClick(再次)恢复状态,然后处理双击。

This feels unsatisfying as a solution, but it works. 作为解决方案,这感觉不尽如人意,但它确实有效。

Using a timer (to swallow the first click of the double click) is equally unsatisfying, and has the added complication of handling user's double click rate preferences. 使用计时器(吞下双击的第一次点击)同样不令人满意,并且处理用户的双击率偏好更加复杂。

Cheers, Rob 干杯,罗布

You could start a timer within the OnMouseClick event handler. 您可以在OnMouseClick事件处理程序中启动计时器。

  • If the timer times out (say 300ms) then a single click is intended. 如果计时器超时(比如300毫秒),那么只需单击一下。

Else 其他

  • If another OnMouseClick event was generated before the time times out you could examine the x&y position of the click and if its within a particular radius of the first click then action the double click functionality. 如果在超时之前生成了另一个OnMouseClick事件,则可以检查点击的x和y位置,如果它在第一次单击的特定半径内,则执行双击功能。

Otherwise 除此以外

  • Handle first click and re-initialise timer for second click 处理首次单击并重新初始化计时器以进行第二次单击

NB: This implementation has the advantage of the fact that both the timeoput and the 'double-click' radius can be configured independantly of the system configuration allowing the s/w to be imported onto multiple machines/ 注意:这种实现的优点在于,timeoput和'double-click'半径都可以独立于系统配置进行配置,允许将s / w导入到多台机器上/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM