Problems simulating mouse clicks using SendMessage
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 click 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.. :mad:
Code:
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.
Re: Problems simulating mouse clicks using SendMessage
Re: Problems simulating mouse clicks using SendMessage
Only the Window that has "Captured the Mouse" will receive its input.
Quote:
From MSDN
The WM_LBUTTONDOWN message is posted when the user presses the left mouse button while the cursor is in the client area of a window. If the mouse is not captured, the message is posted to the window beneath the cursor. Otherwise, the message is posted to the window that has captured the mouse.
I have never tried this but you may need to use the GetCapture, SetCapture and ReleaseCapture api functions as well.
Re: Problems simulating mouse clicks using SendMessage
I will try that, thanks a lot.
Also: I forgot to add, it worked the first time. it clicked the very first time but never again after that
Public Declare Function GetCapture Lib "user32" Alias "GetCapture" () As Long
Public Declare Function SetCapture Lib "user32" Alias "SetCapture" (ByVal hwnd As Long) As Long
Public Declare Function ReleaseCapture Lib "user32" Alias "ReleaseCapture" () As Long
Re: Problems simulating mouse clicks using SendMessage
Well... I don't use Firefox but what is this "option box thing"? You send the click to a window, but isn't it so that you want to send it to a control on that window, like a checkbox or radio button? If so you must have the hWnd of that control and also use the coordinates of that control and not for the window it is placed on.
Re: Problems simulating mouse clicks using SendMessage
It is a radio button. And also, I don't think I need the hWnd of the radio button, since like I said, it worked the first time [but never again].
Re: Problems simulating mouse clicks using SendMessage
A radio button is selected when first clicked, but not deselected when clicked again. To deselect a radio button you're supposed to click on another radio button in the same group. Why don't you try getting the hWnd of the radio button and send a BM_CLICK or a BM_SETCHECK message to it instead?
Re: Problems simulating mouse clicks using SendMessage
If you use Spy++, you will see that .aspx page objects do not have hWnds.
Also, I didn't try to deselect it.. what happened was, my program clicked the button, the page refreshed, and then it wouldn't click anymore after that. I reloaded my IDE, reloaded webpage, everything.
Re: Problems simulating mouse clicks using SendMessage
You didn't mentioned you tried to click a control on a webpage, just that it was in Firefox. I of course assumed you wanted to automate some setting in that application. If you want to set a HTML option you should use the HTML DOM to do so. Search the forum there are many examples available.
Re: Problems simulating mouse clicks using SendMessage
Yes, but I've come this far, why reinvent the wheel?
How can I use the SetCapture API to effectively use my program as I would like to? I tried doing SetCapture(hWndParent) and SetCapture(hWndChild), but neither seem to make my application do anything [like before].
Re: Problems simulating mouse clicks using SendMessage
Quote:
Originally Posted by Tazo
Yes, but I've come this far, why reinvent the wheel?
Because the wheel you have is not suitable for the axle you want to put it on to. You may be able to find a way of making the "wheel" work, but if you can, it would be a huge amount of effort compared to making a new wheel which is suitable.
Re: Problems simulating mouse clicks using SendMessage
Can you please tell me the names of the controls/references I need? -.- HTML DOM is not in any of the lists..
Re: Problems simulating mouse clicks using SendMessage
You will probably need the WebBrowser control, and a reference to "Microsoft HTML Object Library".
Some of the previous threads on this topic should answer most of your questions on how to use them. Here's a couple of useful ones (you can find more with a search):
http://www.vbforums.com/showthread.php?t=404504
http://www.vbforums.com/showthread.php?t=377756