0

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);

Jordan Foreman
  • 3,848
  • 7
  • 40
  • 65
  • 2
    Looks like a duplicate question. See this link. http://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c – Tim Coker Jul 13 '11 at 18:34

2 Answers2

1

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

Marlon
  • 19,924
  • 12
  • 70
  • 101
1

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)
MadBoy
  • 10,824
  • 24
  • 95
  • 156