Is it possible to click programmatically a location in another window without moving the mouse to that location and even if the window is not on-top? I want to send a kind of message to another window to simulate a mouse click on a location.
I tried to accomplish this with PostMessage:
PostMessage(WindowHandle, 0x201, IntPtr.Zero, CreateLParam(300,300));
PostMessage(WindowHandle, 0x202, IntPtr.Zero, CreateLParam(300,300));
I made the CreateLParam function this way:
private static IntPtr CreateLParam(int LoWord, int HiWord)
{
return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
}
The problem is that the window gets locked on his location. I think that my application clicks on the (1,1) coordinate. Can some on help me with this problem?
Edit:
This is PostMessage:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr WindowHandle, int Msg, IntPtr wParam, IntPtr lParam);
And 0x201 and 0x202 are WM_LBUTTONDOWN and WM_LBUTTONUP respectively.
You can’t do that by sending messages, instead use SendInput Windows API.
Call method ClickOnPoint, this is an example from form click event, so
this.handleis form handle, note that these are client coordinates on window witch handle is send, you can easily change this and send screen coordinates, and in that case you don’t need handle or ClientToScreen call below.UPDATE: using SendInput now, tnx Tom.
btw. I used only declarations needed for this sample, for anything more there is a nice library : Windows Input Simulator (C# SendInput Wrapper – Simulate Keyboard and Mouse)