我可以区分系统托盘图标上的单次点击和双次点击吗? [英] Can I differentiate between single and double clicks on the system tray icon?
问题描述
我使用 Shell_NotifyIcon
创建我的系统托盘图标,然后捕获其 WM_LBUTTONDBLCLK
通知,当用户双击图标(我使用它显示一个对话窗口)。我还捕获 WM_RBUTTONDOWN
通知以显示上下文菜单。
I create my system tray icon using Shell_NotifyIcon
and then trap its WM_LBUTTONDBLCLK
notifications for when a user double-clicks the icon (I use it show a dialog window.) I also trap WM_RBUTTONDOWN
notifications to show the context menu.
现在我想,一个左键单击后显示一个上下文菜单是很好。
Now I'm thinking that it would be nice to show a context menu after a single left click. But how do I do that?
如果我捕获 WM_LBUTTONDOWN
并显示我的上下文菜单,它可以正常工作。但是,当有人双击图标时,它首先显示我的上下文菜单,然后显示对话框窗口。
If I trap WM_LBUTTONDOWN
and show my context menu it works fine. But then when someone double-clicks the icon, it first shows my context menu and then displays the dialog window. So I'm not sure how to overcome this?
编辑:这是我的代码:
NOTIFYICONDATA nid;
memset(&nid, 0, sizeof(nid));
nid.cbSize = sizeof(nid);
nid.hWnd = this->GetSafeHwnd();
nid.uID = TRAY_ICON_ID1;
nid.uFlags = NIF_ICON;
nid.uCallbackMessage = TRAY_NOTIFICATION_ID1;
nid.hIcon = ghIcon;
Shell_NotifyIcon(NIM_ADD, &nid);
,然后:
ON_MESSAGE(TRAY_NOTIFICATION_ID1, OnTrayIconNotifications)
LRESULT OnTrayIconNotifications(WPARAM wParam, LPARAM lParam)
{
UINT uID = (UINT)wParam;
UINT uMouseMsg = (UINT)lParam;
if(uID == TRAY_ICON_ID1)
{
switch(uMouseMsg)
{
case WM_RBUTTONDOWN:
{
//Show context menu
//...
int nChosenCmd = TrackPopupMenu(hMenu,
TPM_RIGHTALIGN | TPM_TOPALIGN |
TPM_LEFTBUTTON | TPM_VERPOSANIMATION |
TPM_HORNEGANIMATION | TPM_RETURNCMD,
x, y, 0,
this->GetSafeHwnd(), NULL);
}
break;
case WM_LBUTTONDBLCLK:
{
//Show dialog window
CDialogBasedClass dlg(this);
dlg.DoModal();
}
break;
}
}
return 0;
}
推荐答案
我怀疑你必须
在您的 WM_LBUTTONDOWN
点击处理程序中,设置计时器,该计时器将在系统的双击时间已过。在 WM_LBUTTONDBLCLICK
处理程序中检查该计时器是否处于活动状态,如果是,请取消。
In your WM_LBUTTONDOWN
click handler, set a timer that expires after the system's double click time has elapsed. In your WM_LBUTTONDBLCLICK
handler check if that timer is active and if it is, cancel it.
这意味着用户点击了您的图标,但没有前进并双击它;这意味着它是时候显示上下文菜单。
If the timer expires that means the user clicked your icon but did not go ahead and double-click it; that means it's time to show the context menu.
这篇关于我可以区分系统托盘图标上的单次点击和双次点击吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!