gpt4 book ai didi

c++ - 单击时系统托盘如何跟踪菜单的状态?

转载 作者:行者123 更新时间:2023-11-28 07:16:37 25 4
gpt4 key购买 nike

我有一个系统托盘,我想在单击时跟踪菜单。例如,当在系统托盘上单击鼠标右键时,我使用 InsertMenu(hPopMenu,0xFFFFFFFF,uFlag,IDM_AUTO,_T("Auto")); 但它总是覆盖我的更改?如何跟踪用户点击了哪个菜单?这是我的回调函数:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
POINT lpClickPoint;
HMENU hMenu; // handle to main menu
MENUITEMINFO mii = {sizeof(MENUITEMINFO) };
BOOL status;

switch (message)
{
case WM_USER_SHELLICON:

// systray msg callback
switch(LOWORD(lParam))
{
case WM_RBUTTONDOWN:

UINT uFlag = MF_BYPOSITION|MF_UNCHECKED|MF_STRING;
GetCursorPos(&lpClickPoint);
hPopMenu = CreatePopupMenu();

InsertMenu(hPopMenu,0xFFFFFFFF,uFlag,IDM_AUTO,_T("Auto")); InsertMenu(hPopMenu,0xFFFFFFFF,MF_SEPARATOR,IDM_SEP,_T("SEP"));
InsertMenu(hPopMenu,0xFFFFFFFF,MF_BYPOSITION|MF_STRING,IDM_EXIT,_T("Exit"));

SetForegroundWindow(hWnd);
TrackPopupMenu(hPopMenu,TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_BOTTOMALIGN,lpClickPoint.x, lpClickPoint.y,0,hWnd,NULL);
return TRUE;
}
break;

case WM_COMMAND:

wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);

// Parse the menu selections:


...
...

}
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

最佳答案

您正在重新创建每一次鼠标点击。因此,您必须根据当前状态进行初始化。

UINT uFlag = MF_BYPOSITION|MF_UNCHECKED|MF_STRING;

变成:

UINT uFlag;
if( bCheckboxShouldBeCheckedAccordingToYourApplication )
uFlag = MF_BYPOSITION|MF_CHECKED|MF_STRING;
else
uFlag = MF_BYPOSITION|MF_UNCHECKED|MF_STRING;

这里,现在它更短了,但实际上仍然符合提出的问题:

UINT uFlag = MF_BYPOSITION  | (b?8:0) | MF_STRING;

当然,它的可读性差了一点。

关于c++ - 单击时系统托盘如何跟踪菜单的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20124372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com