Send mouse click

Asked

Viewed 357 times

0

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

  • Take a look here: http://stackoverflow.com/questions/7055211/how-to-get-the-position-of-a-click

  • In this link you can only get(get) the position, I would like to set the position... Move the mouse.

  • You are using Windows Forms?

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. Following 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

  • This method changes the cursor position on the screen and sends the mouse click at that position?

  • I edited the answer, hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.