Creating a mouse click

Wibble

Member
Joined
Jun 27, 2006
Messages
8
Programming Experience
Beginner
I think this is the right place..
Anway, Im using this to move the mouse cursor to a specific location:

Windows.Forms.Cursor.Position = New System.Drawing.Point(x, y)

How can I create a click using the above? (Left click). I have done some looking around forms.cursors and such but I can't find it!
Thanks

-Wj
 
if the cursor is in the position that you want it to be then why do you want to create a click event? the click event will be raised there anyway when you click the mouse all you have to do is catch it.
 
I dont want to have to click it myself, I want to automate it. I need to be able to navigate an external application through mouse clicks. Like send the cursor to a button and then click it, but automatically.
 
Could get complicated. The Api way would be to use ...

FindWindow(..) // To find the window that you want to perform the click in.

Assuming that there will be no child windows... Get the Handle of that window.

else

FindWindowEX // i think is the api to iterate through the child windows to find the control to perform the click on.

Again get the handle of that control.

Then use the sendmessage API to send a click message to that control, which i believe in itsself is comprised of 3 different messages.

Like i say complicated.
 
Well, Ive managed to get hold of this for creating a left click

VB.NET:
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Integer
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Private Const MOUSEEVENTF_LEFTDOWN As Short = &H2S
Private Const MOUSEEVENTF_LEFTUP As Short = &H4S
Private Const MOUSEEVENTF_RIGHTDOWN As Short = &H8S
Private Const MOUSEEVENTF_RIGHTUP As Short = &H10S
Private Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure
Dim pt As POINTAPI
Public Sub LeftMouseClick(ByVal x As Integer, ByVal y As Integer)
SetCursorPos(x, y)
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub

which means i just have to call LeftMouseClick(x, y) for the click. This works, BUT there is another, probably more simple problem.
I need to take into account time it takes forms to load and Ive used someones Wait procedure

VB.NET:
Private Declare Sub Sleep Lib "kernel32" _
(ByVal dwMilliseconds As Long)
Public Sub Wait(ByVal Seconds As Single)
Dim lMilliSeconds As Long
lMilliSeconds = Seconds * 1000
Sleep(lMilliSeconds)
End Sub

Used with Wait(x) but if I have more than one of those in the code, when it is executed they all wait at the same time on the first instance of 'Wait(x)' and then none of the other waits take effect. Any ideas how I can pause my program for a new seconds in various places?
(I guess its the wrong place to ask, but seeing as theres already a thread going here!)
-Wj
 
Agreed, it is always best to try and find a native .Net coding solution before you resort to Win32 API functionality. Which brings me to:
VB.NET:
[SIZE=2][COLOR=#008000]'get position
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] p [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point = System.Windows.Forms.Cursor.Position
[/SIZE][SIZE=2][COLOR=#008000]'set position
[/COLOR][/SIZE][SIZE=2]System.Windows.Forms.Cursor.Position = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(500, 500)
[/SIZE]
 
Beg pardon but..

Have you had a go at using Windows Scripting Host? After all, navigating apps is what it is for. A lot depends upon whether your app controls will receive a focus but if I was going to navigate an app I would give WSH a crak first. You can run WSH from a VbScript/Command Line. I have never tried it from an application but I don't see why you can't.
 
Back
Top