区分C#中的Click事件和DoubleClick事件 [英] Distinguishing between Click and DoubleClick events in C#
问题描述
我目前有一个NotifyIcon作为Windows窗体应用程序的一部分.我想在图标的双击上显示/隐藏表格,并在单击时显示气球提示.我将这两个功能分开工作,但是找不到让应用程序区分单击和双击的方法.现在,它将双击视为两次单击.
I currently have a NotifyIcon as part of a Windows Form application. I would like to have the form show/hide on a double click of the icon and show a balloon tip when single clicked. I have the two functionalities working separately, but I can't find a way to have the app distinguish between a single click and double click. Right now, it treats a double click as two clicks.
如果检测到第二次单击,是否可以阻止单击事件?
Is there a way to block the single click event if there is a second click detected?
推荐答案
不幸的是,对 NotifyIcon
类的 MouseClick
事件建议的处理方法不起作用-在我的测试中 e.MouseClicks
始终为0,也可以从
Unfortunately the suggested handling of MouseClick
event doesn't work for NotifyIcon
class - in my tests e.MouseClicks
is always 0, which also can be seen from the reference source.
我看到的相对简单的方法是通过使用表单级别标志, async
处理程序和 Task.Delay
延迟对 Click
事件的处理.代码>:
The relatively simple way I see is to delay the processing of the Click
event by using a form level flag, async
handler and Task.Delay
:
bool clicked;
private async void OnNotifyIconClick(object sender, EventArgs e)
{
if (clicked) return;
clicked = true;
await Task.Delay(SystemInformation.DoubleClickTime);
if (!clicked) return;
clicked = false;
// Process Click...
}
private void OnNotifyIconDoubleClick(object sender, EventArgs e)
{
clicked = false;
// Process Double Click...
}
唯一的缺点是在我的环境中, Click
的处理被延迟了半秒( DoubleClickTime
为500毫秒).
The only drawback is that in my environment the processing of the Click
is delayed by half second (DoubleClickTime
is 500 ms).
这篇关于区分C#中的Click事件和DoubleClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!