问题描述
我有一个WPF任务栏一样,我很开心开发应用程序,我希望能够打开系统菜单的应用程序(即在我的自定义任务栏),我用鼠标右键单击,保留任何自定义菜单的应用程序可能会创建(即谷歌浏览器)。我有以下的代码,对于一个没有国界的窗口程序,我以前所作的作品,但它似乎没有工作,我想知道为什么吗?和什么我需要做的就是它的工作?
I have a WPF TaskBar like application that i am developing for fun and i want to be able to open the System Menu for the application (that is in my custom taskbar) that i right click on, preserving any custom menu that the app might create (i.e. Google Chrome). I have the following code that works for a borderless window app i made previously, but it doesn't seem to work and i am wondering why? And what do i need to do to get it to work?
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,而不是等待响应...
Tip: It appears that TrackPopupMenuEx(...) returns immediately with the value of 0, instead of waiting for a response...
推荐答案
这似乎有一个问题,与给予TrackPopupMenuEx所有者窗口句柄在菜单的......相反,我用我的WPF窗口的句柄,然后张贴的消息时,我用了菜单的主人......似乎有点怪我,但它的作品!
It appears there is an issue with giving TrackPopupMenuEx the owner window handle of the Menu... instead i used the handle of my wpf window and then when posting the message, i used the Menu's owner... seems a bit strange to me but it works!
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);
}
这篇关于打开另一个应用程序的系统菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!