SendMessage ButtonUp/Down to handle x/y coordinates

1.6k views Asked by At

I am in the process of expanding a small bot I made for "clicker" browser games (clicker heroes, candy clicker, etc.)

Originally, I was sending clicks like this:

 var screenPoint = Cursor.Position;
 var handle = WindowFromPoint(screenPoint);
 if (handle != IntPtr.Zero)
 {
     SendMessage(handle, Win32.WM_RBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
     SendMessage(handle, Win32.WM_RBUTTONUP, IntPtr.Zero, IntPtr.Zero);              
 }

But this obviously causes the problem of the window needing to be in front.

I want to be able to send clicks to the window handle itself, so I get the handle like so:

winHandle = Win32.FindWindow("PlayerClient", "Clicker Heroes");
SendMessage(winHandle , Win32.WM_RBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
...

The problem now is, how do I specify the Point or xy-coordinates that I wish it to send the click to? The documentation mentions something about the LParam taking a Point, but doesn't provide any examples.

Thanks for your help.

1

There are 1 answers

0
Kyle On

From:

You need to pass the coordinates to SendMessage via the lParam parameter.

LRESULT CALLBACK SendMessage(
  _In_ HWND   hwnd,
  _In_ UINT   uMsg,
  _In_ WPARAM wParam,
  _In_ LPARAM lParam
);

lParam

The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

If you want a helper function to build the LPARAM I suggest starting with the following for a 32 bit program:

public static LPARAM BuildLParam(ushort low, ushort high)
{
    return (LPARAM)(((uint)high << 16) | (uint)low);
}