Click at specified client area

Posted by VixinG on Stack Overflow See other posts from Stack Overflow or by VixinG
Published on 2012-11-25T04:48:13Z Indexed on 2012/11/25 5:03 UTC
Read the original article Hit count: 188

Filed under:
|
|
|

Click doesn't work - I don't know why and can't find a solution :(

ie. Click(150,215) should move mouse to the client area and click there.

    [DllImport("user32.dll")]
    private static extern bool ScreenToClient(IntPtr hWnd, ref Point lpPoint);

    [DllImport("user32", SetLastError = true)]
    private static extern int SetCursorPos(int x, int y);

    static void MouseMove(int x, int y)
    {
        Point p = new Point(x * -1, y * -1);
        ScreenToClient(hWnd, ref p);
        p = new Point(p.X * -1, p.Y * -1);
        SetCursorPos(p.X, p.Y);
    }

    static void Click(int x, int y)
    {
        MouseMove(x, y);
        SendMessage(hWnd, WM_LBUTTONDOWN, (IntPtr)0x1, new IntPtr(y * 0x10000 + x));
        SendMessage(hWnd, WM_LBUTTONUP, (IntPtr)0x1, new IntPtr(y * 0x10000 + x));
    }

Edit: Of course I can use mouse_event for that, but I would like to see a solution for SendMessage()...

    [DllImport("user32.dll")]
    static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

    const int LEFTDOWN = 0x00000002;
    const int LEFTUP = 0x00000004;

    static void Click(int x, int y)
    {
        MouseMove(x, y);
        mouse_event((int)(LEFTDOWN), 0, 0, 0, 0);
        mouse_event((int)(LEFTUP), 0, 0, 0, 0);
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about click