This article was previously published under Q135788
When you display a context menu for a notification icon (seeShell_NotifyIcon), clicking anywhere besides the menu or the window thatcreated the menu (if it is visible) doesn't cause the menu to disappear.When this behavior is corrected, the second time this menu is displayed, itdisplays and then immediately disappears.
To correct the first behavior, you need to make the current window theforeground window before calling TrackPopupMenu or TrackPopupMenuEx.If the current window is a child window, set the (top-level) parent window as the foreground window.
The second problem is caused by a problem with TrackPopupMenu. It isnecessary to force a task switch to the application that calledTrackPopupMenu at some time in the near future. This can be accomplished byposting a benign message to the window or thread.
The following code will take care of all of this:
SetForegroundWindow(hDlg); // Display the menu TrackPopupMenu( hSubMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hDlg, NULL); PostMessage(hDlg, WM_NULL, 0, 0);