I made a little app in C++ and I have two pairs of menus; one for the main window, and one for the tray icon I put there. I'm trying to put a checkmark next to a specific menu item in the tray icon context menu, but I can't get this to work.
if (!isCheckmarked)
{
CheckMenuItem(cSubMenu, IDC_STARTWIN, MF_CHECKED);
OutputDebugString(_T("Checkmarked!\n"));
_RPT2(_CRT_WARN, "Menu Handles: %i, %i\n", cMenu, cSubMenu);
isCheckmarked = TRUE;
}
else
{
//CheckMenuItem(hMenu, IDC_STARTWIN, MF_UNCHECKED);
OutputDebugString(_T("Uncheckmarked!\n"));
//_RPT2(_CRT_WARN, "Menu Handles: %i, %i\n", cMenu, cSubMenu);
isCheckmarked = FALSE;
}
break;
It works fine, if I use the menu handle of the main window to set a checkmark on one of those items, but I can't get it to work on the tray icon context menu.
void ShowContextMenu(HWND hWnd, POINT pt)
{
cMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDC_CONTEXT_MENU));
if (cMenu)
{
cSubMenu = GetSubMenu(cMenu, 0);
if (cSubMenu)
{
// our window must be foreground before calling TrackPopupMenu or the menu will not disappear when the user clicks away
SetForegroundWindow(hWnd);
// respect menu drop alignment
UINT uFlags = TPM_RIGHTBUTTON;
if (GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0)
{
uFlags |= TPM_RIGHTALIGN;
}
else
{
uFlags |= TPM_LEFTALIGN;
}
TrackPopupMenuEx(cSubMenu, uFlags, pt.x, pt.y, hWnd, NULL);
}
DestroyMenu(cMenu);
}