I'm trying to send a mouse click to a certain position on a
*background* window. Nothing is happening, so as a test I'm just
trying to move the mouse to that position. Still nothing is happening.
What am I doing wrong?
I've looked at mouse_event, but that only allows clicking a certain
position on the screen, or a relative offset from the current mouse
position. I don't see how to click a certain position on a given
window with mouse_event.
Declare Function PostMessage Lib "user32.dll" _
Alias "PostMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_MOUSEMOVE As Long = &H200
Dim xcoord As Long
Dim ycoord As Long
Dim packed As Long
xcoord = 350
ycoord = 249
packed = (ycoord * &H10000) + xcoord
Call PostMessage(hWnd, WM_MOUSEMOVE, 0&, packed)
==============
As a sanity check, this code DOES move the mouse to the correct
location on the window and click there, if the window is active:
MoveMouse hWnd, 350, 249
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&
Sub MoveMouse(ByVal hWnd As Long, h_offset, v_offset)
Dim lpRect As RECT
GetWindowRect hWnd, lpRect
SetCursorPos lpRect.Left + h_offset, lpRect.Top + v_offset
End Sub
Thanks for any suggestions.
Greg
Scott Seligman [MSFT] - 23 Apr 2007 23:49 GMT
>Call PostMessage(hWnd, WM_MOUSEMOVE, 0&, packed)
You're only sending a mouse move. Alone this will do nothing with most
applications. If you want a click, you need to send a WM_LBUTTONDOWN
followed by a WM_LBUTTONUP to simulate the user pressing and releasing
the left mouse button.
Even this may not work however. If you're attempting to click on a
button, the application is unlikely to care about mouse clicks that
happen to the main window under the button's window, so you'll need
to send the even to the button's window. That, of course, assumes the
application actually has a window for each button it shows. Some apps,
like Internet Explorer, paint the buttons themselves, so you'll need to
treat them differently.
Windows as a whole doesn't really support the concept of creating a
separate mouse or keyboard queue that you can access per application, so
whatever solution you come up with will likely need tweaking for each
application you use it on.

Signature
Scott Seligman [MSFT]
This posting is provided AS IS with no warranties, and confers
no rights.