Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Tazo on May 14, 2006, 11:21 AM

Title: Simulating mouse clicks using SendMessage
Post by: Tazo on May 14, 2006, 11:21 AM
I have been trying this for around 2 and a half hours now, to no avail. For whatever reason, the program I am sending the keys to ( Firefox ) isn't reacting. It is supposed to click one of those option box things. I am totally clueless as to why it won't work..  >:( I got it to work once, out of the blue, and it never worked again.


Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
 
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const GW_CHILD = 5

Private hWndParent As Long
Private hWndChild As Long
'-----------------------------------------------------------------------------'

Public Sub SendClick(lnghWND As Long, x As Long, y As Long)

    Dim iResult As Long
    Dim lParam As Long
   
    lParam = (y * &H10000) + x
    Debug.Print "lParam is: " & lParam & " || lnghWND is: " & lnghWND & " || x is: " & x & " || y is: " & y

    iResult = SendMessage(lnghWND, WM_LBUTTONDOWN, 0&, ByVal lParam)
    iResult = SendMessage(lnghWND, WM_LBUTTONUP, 0&, ByVal lParam)
   
End Sub
Private Sub Command2_Click()

    Debug.Print hWndChild
    Call SendClick(hWndChild, 296, 701)

End Sub

Private Sub Form_Load()

    hWndParent = FindWindow(vbNullString, "Index - Mozilla Firefox")
    hWndChild = GetWindow(hWndParent, GW_CHILD)
    Debug.Print "parent: " & hWndParent
    Debug.Print "child: " & hWndChild

End Sub

I am always getting a correct parent/child value, so that isn't the problem. I am also positive that my X,Y coords are correct.

Any help is appreciated.
Title: Re: Simulating mouse clicks using SendMessage
Post by: topaz on May 14, 2006, 01:25 PM
I was working with Mozilla earlier, FindWindow("MozillaUIWindowClass", vbNullString) is the right one to use
Title: Re: Simulating mouse clicks using SendMessage
Post by: Tazo on May 14, 2006, 01:35 PM
Yea, I saw that in Spy++, however, the window handle is not my problem, I derive that fine [child and parent].

Edit: Heh,
FindWindow("MozillaUIWindowClass", vbNullString)
and my way both produce the same results. *shrug*
Title: Re: Simulating mouse clicks using SendMessage
Post by: Tazo on May 14, 2006, 09:51 PM
Ok, how about this: How would I go about clicking on a radio button that is in an .aspx page in FireFox?
Title: Re: Simulating mouse clicks using SendMessage
Post by: Grok on May 15, 2006, 04:18 PM
Quote from: Tazo on May 14, 2006, 11:21 AM


    Debug.Print "lParam is: " & lParam & " || lnghWND is: " & lnghWND & " || x is: " & x & " || y is: " & y

    iResult = SendMessage(lnghWND, WM_LBUTTONDOWN, 0&, ByVal lParam)
    iResult = SendMessage(lnghWND, WM_LBUTTONUP, 0&, ByVal lParam)

I am always getting a correct parent/child value, so that isn't the problem. I am also positive that my X,Y coords are correct.

Any help is appreciated.

Check the value of iResult on both calls.  Further, get the window properties of the lnghWND including any text/caption to help you verify you really do have the correct window handle.

You could also watch the mouse-class messages using SPY++ specifically to those buttons, and click them manually see which which messages are firing.
Title: Re: Simulating mouse clicks using SendMessage
Post by: Grok on May 15, 2006, 04:25 PM
Quote from: Tazo on May 14, 2006, 09:51 PM
Ok, how about this: How would I go about clicking on a radio button that is in an .aspx page in FireFox?

BM_CLICK maybe.

BM_CLICK - simulate the user clicking a button.
BM_GETCHECK - retrieve the check state of a radio button or check box.
BM_SETCHECK - set the check state of a radio button or check box.
Title: Re: Simulating mouse clicks using SendMessage
Post by: Tazo on May 15, 2006, 10:37 PM
I resorted to using a WebBrowser control and then using the .Click command. Thanks anyways though..
Title: Re: Simulating mouse clicks using SendMessage
Post by: Joe[x86] on May 16, 2006, 07:09 AM
Gross. =p