使用sendmessage(或类似功能)单击上下文菜单项 [英] click a context menu item using sendmessage (or similar function)
问题描述
我需要右键单击另一个应用程序,获取它的上下文菜单(在右键单击后打开),然后从中选择一个项目。
I need to right click on another application, gets it context menu (that was opened after the right click), and than select an item from it.
I可以将postMessage与其他应用程序句柄一起使用,结果确实出现了请求的上下文菜单,但是我不知道如何选择它。
I can use the postMessage with the other application handle, and as a results the requested context menu did appear, but I have no idea of how to select from it.
public const int WM_RBUTTONDOWN = 0x0204;
public const int WM_RBUTTONUP = 0x0205;
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern void SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "PostMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
Point p = Cursor.Position;
PostMessage((IntPtr)123456, WM_RBUTTONDOWN, 0, 0);
PostMessage((IntPtr)123456, WM_RBUTTONUP, 0, 0);
下一步我应该做什么(现在上下文菜单已打开)?
what should I do next (now the context menu is open)?
谢谢,
Tomer。
thanks, Tomer.
推荐答案
我想您想触发特定操作。无需弹出上下文菜单:只需发布 WM_COMMAND 与您要选择的上下文菜单项匹配的消息。您可以使用Spy ++或 Winspector 之类的工具找到与所需菜单项关联的WM_COMMAND项ID。
I guess you want to trigger a specific action. No need to popup the context menu: Just post a WM_COMMAND message that matches the context menu item you want to select. You'll find the WM_COMMAND item id associated to the desired menu item using tools such as Spy++ or Winspector.
PostMessage((IntPtr)hWnd, WM_COMMAND, 0, ID_MENU_ITEM);
编辑:澄清您的评论:
您直接将WM_COMMAND消息发送或发布到主窗口,而不是菜单。实际上,您根本不需要弹出菜单。上下文菜单只是一个GUI元素,允许用户选择一个动作。该应用程序不需要显示菜单即可传达上述操作。
You send or post the WM_COMMAND message straight to the main window, not to the menu. Actually, you don't need to popup the menu at all. The context menu is just a GUI element that lets the user select an action. The application doesn't need the menu to show up in order to convey said action.
这篇关于使用sendmessage(或类似功能)单击上下文菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!