I'm trying to show the system menu (containing minimize, restore, etc.) from a different process in my WinForms UI. I understand that I need interop calls like GetSystemMenu and TrackPopupMenuEx but I failed to make it work. Can someone provide a sample code how to do it?
I've found this code snippet (for WPF): Open another application's System Menu
I modified it to something like this:
const uint TPM_LEFTBUTTON = 0x0000;
const uint TPM_RETURNCMD = 0x0100;
const uint WM_SYSCOMMAND = 0x0112;
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern uint TrackPopupMenuEx(IntPtr hmenu, uint fuFlags, int x, int y, IntPtr hwnd, IntPtr lptpm);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public void ShowContextMenu()
{
IntPtr wMenu = GetSystemMenu(ExternalWindowHandle, false);
// Display the menu
uint command = TrackPopupMenuEx(wMenu, TPM_LEFTBUTTON | TPM_RETURNCMD, 10, 10, ExternalWindowHandle, IntPtr.Zero);
if (command == 0)
return;
PostMessage(ExternalWindowHandle, WM_SYSCOMMAND, new IntPtr(command), IntPtr.Zero);
}
As mentioned in the question title, I do not want to minimize a window to the systray, I want to display a system menu from another process (window) at a location I choose. Pretty much the same way as the windows taskbar. The taskbar (explorer) seems to be able to display the system menu when you right-click it on the taskbar.
Thanks, Stefan