-2

I tried How to simulate Mouse Click in C#?

public void DoMouseClick(uint X, uint Y)
    {
        //Call the imported function with the cursor's current position
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }

My problem is, that the click got executed on that point, my cursor is and not on that point, I set for x and y. My coordinates aren“t wrong. I tried it also with x=1 and y=1.

Community
  • 1
  • 1
ElDiabolo
  • 357
  • 5
  • 20

1 Answers1

1
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool SetCursorPos(int x, int y);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

    public const int MOUSEEVENTF_LEFTDOWN = 0x0002;//0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x0004;//0x04;

    public void Click(int x, int y)
    {
        Thread.Sleep(2000);
        SetCursorPos(x, y);
        Thread.Sleep(1000);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        Thread.Sleep(1000);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }
Narek Arzumanyan
  • 616
  • 3
  • 11