Send mouse click

0

I would like to know how to send a mouse click on the screen by passing the click (x, y) position coordinates per parameter in C #.

    
asked by anonymous 31.07.2015 / 20:24

1 answer

4

If you are using Windows Forms, to move the cursor use the class Cursor .

        this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(Cursor.Position.X - positionX, Cursor.Position.Y - positionY);
        Cursor.Clip = new Rectangle(this.Location, this.Size);

To move and click, you will need to import the Windows library. Here's an example below:

    [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 = 0x02;
    public const int MOUSEEVENTF_LEFTUP = 0x04;


    private void mouseClick(int x, int y)
    {
        SetCursorPos(x, y);
        mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    }

References

    
31.07.2015 / 20:38