Click a context menu item using sendmessage (or a similar function)

I need to right-click on another application, get the context menu (open after right-clicking) and select an item from it.

I can use postMessage with another application descriptor, and as a result the requested context menu appears, but I have no idea how to select 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); 

code>

what should I do next (now the context menu is open)?

thanks, tomer.

+1
source share
1 answer

I assume that you want to trigger a specific action. No need to pop up the context menu: just post the WM_COMMAND message that corresponds to the context menu item that you want to select. You will find the WM_COMMAND object identifier associated with the desired menu item using tools such as Spy ++ or Winspector .

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

EDIT: Clarification in response to your comment:

You send or send the WM_COMMAND message directly to the main window, and not to the menu. In fact, you don’t have to pop up a menu at all. The context menu is just a GUI element that allows the user to select an action. An application does not need to display a menu to convey the specified action.

0
source

All Articles