Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Fr0z3N on February 01, 2006, 04:34 PM

Title: Input mouse clicks into a running program
Post by: Fr0z3N on February 01, 2006, 04:34 PM
What I wanna do is input mouseclicks into a program I've hooked into, I have no idea how to do this or what api to use. So any tips are appreciated.
Title: Re: Input mouse clicks into a running program
Post by: l2k-Shadow on February 01, 2006, 05:41 PM
well you can always use SendMessage (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/sendmessage.asp).
If that's not what you're looking for try SetCursorPos (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/cursors/cursorreference/cursorfunctions/setcursorpos.asp) and mouse_event (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputfunctions/mouse_event.asp).
Title: Re: Input mouse clicks into a running program
Post by: Fr0z3N on February 01, 2006, 10:44 PM
Yeah I'm trying to use the SendMessage API but I don't know how to send in the co-ordinates of where I wanna click.

The thing I'm stumpted on is what to send for lParam, you need the send the co-ordinates but I dunno how.. I've tryed X and Y, X & Y, X & " " & Y, tryed putting them in a type and a few other things.
Title: Re: Input mouse clicks into a running program
Post by: FrOzeN on February 02, 2006, 12:44 AM
Declare:
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long

Usage:
SetCursorPos X, Y

If you want to move it to a certain location on another app, assuming you have that application's hWnd you can do:

Declares:
Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long


Usage:
Dim lpPoint As POINTAPI
lpPoint.X = egX
lpPoint.Y = egY
ClientToScreen eghWnd, lpPoint
SetCursorPos lpPoint.X, lpPoint.Y

(Replacing egX with Left, egY with Top and eghWnd with the application's hWnd.)

Hope that helps. :)

Not too sure about sending mouse clicks.

[EDIT] Oops, didn't read post above correctly so you probably already know this, though, the ClientToScreen might be handy if your simulating your events on another program.

[EDIT 2] I was just mucking around with the mouse_event thingy l2k-Shadow said. With the following code I was able to make it click my winamp play button. Maybe with some modifying you'll be able to get it working for you.

Option Explicit

Private Type POINTAPI
   X As Long
   Y As Long
End Type

Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Private Sub Form_Load()
    Dim hWnd_Winamp As Long
    hWnd_Winamp = FindWindow("Winamp v1.x", vbNullString)
    If hWnd_Winamp <> 0 Then
        Dim lpPoint As POINTAPI
        lpPoint.X = 50
        lpPoint.Y = 90
        ClientToScreen hWnd_Winamp, lpPoint
        SetCursorPos lpPoint.X, lpPoint.Y
        mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_ABSOLUTE, 50, 90, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP Or MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0
    End If
End Sub

The downside was that Winamp had to be the Topmost program. If it was minimized or had a program infront of it then it would just send a LeftClick to the co-ordinates 0, 0 on my screen.