使用sendmessage(或类似函数)单击上下文菜单项

时间:2009-05-25 14:01:48

标签: c# contextmenu

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

我可以将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);

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

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

感谢, 托梅尔。

1 个答案:

答案 0 :(得分:0)

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

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

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

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