我有一个像我正在开发的应用程序的WPF TaskBar,我希望能够打开我右键单击的应用程序的系统菜单(在我的自定义任务栏中),保留任何自定义菜单应用可能会创建(即谷歌浏览器)。我有以下代码适用于我之前制作的无边框窗口应用程序,但它似乎不起作用,我想知道为什么?我需要做些什么才能让它发挥作用?
public static void ShowContextMenu(IntPtr hWnd)
{
SetForegroundWindow(hWnd);
IntPtr wMenu = GetSystemMenu(hWnd, false);
// Display the menu
uint command = TrackPopupMenuEx(wMenu,
TPM.LEFTBUTTON | TPM.RETURNCMD, 0, 0, hWnd, IntPtr.Zero);
if (command == 0)
return;
PostMessage(hWnd, WM.SYSCOMMAND, new IntPtr(command), IntPtr.Zero);
}
提示:TrackPopupMenuEx(...)似乎立即返回值0,而不是等待响应......
答案 0 :(得分:1)
看来有一个问题是给予TrackPopupMenuEx菜单的所有者窗口句柄...而是我使用了我的wpf窗口的句柄,然后在发布消息时,我使用了菜单的所有者......似乎有点奇怪对我而言它有效!
public static void ShowContextMenu(IntPtr hAppWnd, Window taskBar, System.Windows.Point pt)
{
WindowInteropHelper helper = new WindowInteropHelper(taskBar);
IntPtr callingTaskBarWindow = helper.Handle;
IntPtr wMenu = GetSystemMenu(hAppWnd, false);
// Display the menu
uint command = TrackPopupMenuEx(wMenu,
TPM.LEFTBUTTON | TPM.RETURNCMD, (int) pt.X, (int) pt.Y, callingTaskBarWindow, IntPtr.Zero);
if (command == 0)
return;
PostMessage(hAppWnd, WM.SYSCOMMAND, new IntPtr(command), IntPtr.Zero);
}