Morten Snedker wrote:
Hit me! :-)
It's been a long time since I've done this, but it involves sending the
Windows messages directly to the control that it would normally
receive.
First, you need the handle (hWnd) of the control:
1) Get the visible control for a given screen location using API
WindowFromPoint (easy), or
2) Call API GetWindow recursively to search for a control by
WindowName/ClassName/Text (harder, but you can "click" things not even
visible)
Then, use API PostMessage to send the appropriate messages to that
hWnd. You can see the messages that a control receives during normal
mouse/keyboard interaction by using a tool like WinSpy to intercept and
display them.
Here's where my memory gets a bit fuzzy. If I recall, a few controls
will respond just fine to a single WM_CLICK message, but most prefer a
WM_LBUTTONDOWN followed by a WM_LBUTTONUP. I found some code in an old
VB6 program of mine, and removed the non-relevant stuff; this should at
least give you the idea:
-----
Private Type LongInt
lng As Long
End Type
Private Type LoHiWord
lw As Integer
hw As Integer
End Type
Sub LeftClickAt(ByVal hWnd as Long, ByVal X as Long, ByVal Y as Long)
' virtual click left mouse button (I think X/Y were control
coordinates, not screen)
SendMouseEvent hWnd, WM_MOUSEMOVE, X, Y
SendMouseEvent hWnd, WM_LBUTTONDOWN, X, Y
SendMouseEvent hWnd, WM_LBUTTONUP, X, Y
SendMouseEvent hWnd, WM_MOUSEMOVE, X, Y
End Sub
Private Function SendMouseEvent(ByVal hWnd As Long, ByVal lMsg As Long,
ByVal nX As Long, ByVal nY As Long) As Long
' Sends a virtual mouse message directly to a window.
Dim l1 As Long
Dim li As LongInt
Dim lhw As LoHiWord
lhw.lw = nX
lhw.hw = nY
LSet li = lhw
SendMouseEvent = PostMessage(hWnd, lMsg, ByVal l1, ByVal li.lng)
End Function
-----
Let me know if you get stuck and I'll dig up some more info.