How do you programmatically click the mouse?

malitice

Active member
Joined
Feb 20, 2005
Messages
41
Programming Experience
1-3
I can program it in vb6 but I can't seem to find information on doing it in vb.net. I don't need anything fancy just the code to click the left mouse button.
 
No that is for programatically clicking a button in the program. I need the code for clicking the mouse button on whatever it is currently hovering over so that the program thinks the mouse clicked without me having to hit the mouse button.
 
Could you rephrase the question please? What exactely you want to happen after you hovered and click was performed over say textbox1 and button2?

meanwhile:
from your original thread i noted you just want to catch left mouse click ... that's an easy task ....

VB.NET:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
 
If e.Button = MouseButtons.Left Then
 
[size=2]MessageBox.Show[/size]("You just clicked the mouse left button")

 
End If
 
End Sub


Cheers ;)
 
Last edited:
as moderator has give a function {x.PerformClick()}
so asume u have a button so use [mouse move event of that button
simply add <buttonname.performclick()>

it works
 
malitice, I take it you aren't wanting to click a button. You simply want to be able to perform a mouse click at any location on the screen. This can be done by adding the following method to your code:

DeclareSub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags AsLong, ByVal dx AsLong, ByVal dy AsLong, ByVal cButtons AsLong, ByVal dwExtraInfo AsLong)

After adding this you can control mouse events from inside your other methods by calling mouse_event. An example may be something like this:

PrivateSub MyMethod()

Windows.Forms.Cursor.Current.Position = New System.Drawing.Point(225, 105)

mouse_event(&H2, 0, 0, 0, 1)

EndSub

This would allow you to set the cursor position and perform a left mouse click. The different mouse click options (right click, left click, etc.) are controlled by the first parameter of the function. In this case &H2 programatically implements a left mouse down at the x-y position 225,105. Other mouse events may be implemented using these:

&H1 is a mouse move
&H2 is left mouse button down
&H4 is left mouse button up
&H8 is right mouse button up
&H10 is right mouse button down
&H20 is middle mouse button down
&H40 is middle mouse button up

Hope this helps!

Barry
 
How would you do this using VB 2005?

At least, I tried copying in the DeclareSub part from BD7117, but it didn't seem like it recognized AsLong so I thought it must be for another VB.

Thanks,
Annoy Mous
 
Last edited:
Alright, thanks for that help it cleared up a little bit, but now I get a PInvokeStackImbalance when I run it, and it points me to the mouse_event(&H2, 0, 0, 0, 1). (I put the line in a button click code, dunno if that is affecting it.)

 
Last edited:
I'm having the same problem, and I'm not sure what he means by release it?

I placed this below the first mouse_event of left clicking:

mouse_event(&H4, 0, 0, 0, 1)

To simulate the mouse up, but still get the error?
 
instead, use the API to send the relevant control a message saying it has been clicked on. No need to move the mouse there and click..
 
I'm not sure that would be true... Its been a long time since I fiddled with API stuff, but I think even a fullscreen 3d app would have a message loop into which you can post.. JohnH is pretty good with techy internal stuff.. As are the guys over on CodeGuru.. You can probably even ask in the C++ forum there, and then use their advice in combination with a few api calls from a VB.NET tutorial..
 
Back
Top