SendMessage with Point not working

716 views Asked by At

I feel really stupid because I can't find the problem in my code... I want to send mouse clicks with SendMessage. So my code is the following

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

private void MouseClick(IntPtr hWnd, int X, int Y)
{
    SendMessage(hWnd, WM_LBUTTONDOWN, (IntPtr)0x1, CreateLParam(X, Y));
    SendMessage(hWnd, WM_LBUTTONUP, (IntPtr)0x0, CreateLParam(X, Y));
}

private void MouseClick(IntPtr hwnd, System.Drawing.Point p)
{
    SendMessage(hWnd, WM_LBUTTONDOWN, (IntPtr)0x1, CreateLParam(p.X, p.Y));
    SendMessage(hWnd, WM_LBUTTONUP, (IntPtr)0x0, CreateLParam(p.X, p.Y));
}

and CreateLParam is just a function to convert x, y coordinates in LParam:

private static IntPtr CreateLParam(int x, int y)
{
    return (IntPtr)(x | (y << 16));
}

As you can see I want to call this method with a point because it seems easier to me than always having two variables varX and varY (and would like to change it in CreateLParam too if it's working...) The problem now is, that the call

int X = //some value here
int Y = //some value here
MouseClick(appWnd, X, Y);

is working but the call

int X = //some value here
int Y = //some value here
var p = new System.Drawing.Point(X, Y);
MouseClick(appWnd, p);

is not working and I can't see why. Already tried to debug but it's just not sending any message to the window. If i call it with p.X and p.Y it works again as exptected. Not a big problem because I could do it with x and y values but it's more code for me :P btw. tried it with own point struct too.

So am I missing something?

0

There are 0 answers