-
Display system menu
I am trying to make a system menu display from another application at the current position of cursor.(though currently I can not get it to display at all)
Exactly the same as when you right click on a a task in the task bar at the bottom of screen.
I have the window handle of the other application.
The following code is executed when the event WM_CONTEXTMENU is received(i know it passes here as it displays a message box if i put it in there.
Code:
HMENU hMenu;
hMenu = GetSystemMenu(testItem->hWnd, 0);
TrackPopupMenuEx(hMenu, TPM_LEFTBUTTON || TPM_LEFTALIGN, 50, 50, testItem->hWnd, NULL);
It compiles fine but i get no menu.
I can not get it to work for the life of me.
-
This is how to do it
Code:
HMENU hmenu = GetSystemMenu(testItem->hWnd, FALSE);
if (hmenu) {
POINT pt;
GetCursorPos(&pt);
int cmd = TrackPopupMenu(hmenu, TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_RETURNCMD, pt.x, pt.y, 0, hwnd, NULL);
if (cmd) {
SetForegroundWindow(testItem->hWnd); // reactivate window after the context menu has closed
PostMessage(testItem->hWnd, WM_SYSCOMMAND, cmd, 0);
}
}