Jordan Foreman
Jordan Foreman

Reputation: 3888

Automating MouseClick in C#

I am very new to winforms and automation in general, and was wondering if there is a simple way to force the cursor to click in C#. I am already moving the cursor to the right spot, and I want to make it perform a click. Is there code that will do this, without getting super complicated. (for clarity, I am automating a mouseclick on a button in another application, which has been opened at this point)

Here is how I set the cursor position:

Cursor.Position = new Point(x, y);

Upvotes: 0

Views: 2693

Answers (2)

MadBoy
MadBoy

Reputation: 11104

I think you should try AutoIt v3 instead unless you specifically are bound to C#. It's designed for this kind of automation.

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!

For example:

; Double click at the current mouse pos MouseClick("left") 
MouseClick("left")

; Double click at 0,500 
MouseClick("left", 0, 500, 2)   

; SAFER VERSION of Double click at 0,500 - takes into account user's control panel settings 
MouseClick("primary", 0, 500, 2)

Upvotes: 1

Marlon
Marlon

Reputation: 20312

You will want to pass MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP to the mouse_event function.

Upvotes: 1

Related Questions