使用 sendmessage (或类似功能)单击上下文菜单项

发布于 2024-07-20 15:20:34 字数 973 浏览 5 评论 0原文

我需要右键单击另一个应用程序,获取它的上下文菜单(右键单击后打开),然后从中选择一个项目。

我可以将 postMessage 与其他应用程序句柄一起使用,结果确实出现了请求的上下文菜单,但我不知道如何从中进行选择。

    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);

接下来我应该做什么(现在上下文菜单已打开)?

谢谢, 托默.

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 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)?

thanks,
Tomer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2024-07-27 15:20:34

我猜你想触发一个特定的动作。 无需弹出上下文菜单:只需发布 WM_COMMAND 与您要选择的上下文菜单项匹配的消息。 您将使用 Spy++ 或 Winspector 等工具找到与所需菜单项关联的 WM_COMMAND 项 ID 。

PostMessage((IntPtr)hWnd, WM_COMMAND, 0, ID_MENU_ITEM);

编辑:澄清回答您的评论:

您将 WM_COMMAND 消息直接发送或发布到主窗口,而不是菜单。 实际上,您根本不需要弹出菜单。 上下文菜单只是一个 GUI 元素,可让用户选择操作。 应用程序不需要显示菜单来传达所述操作。

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);

EDIT: Clarification in answer to your comment:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文