Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" ( _
ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2
Private Const WM_LBUTTONDOWN = &H201 'Button down
Private Const WM_LBUTTONUP = &H202 'Button up
Private Sub Command1_Click()
Dim lngHandle1 As Long
Dim lngButtonHdl As Long
lngHandle1 = pID2hWnd(Shell("AppB.exe", vbHide))
If (lngHandle1 > 0) Then
PressHandle FindWindowEx(lngHandle1, 0&, "Button", "OK")
End If
End Sub
Private Function pID2hWnd(ByVal pID As Long) As Long
'the Shell() function returns the Process ID (PID) for the newly spawned
'process. Below we loop through every top-level window, get it's PID and
'compare it to the one returned by the Shell() function. If it's the same,
'we've found the window-handle (hWnd) to it!
Dim lngHandle As Long, lngProcID As Long
lngHandle = GetDesktopWindow
lngHandle = GetWindow(lngHandle, GW_CHILD)
Do
If GetWindowThreadProcessId(lngHandle, lngProcID) <> 0 Then
If lngProcID = pID Then
pID2hWnd = lngHandle
Exit Do
End If
End If
lngHandle = GetWindow(lngHandle, GW_HWNDNEXT)
Loop Until lngHandle = 0
End Function
Private Sub PressHandle(ByVal lHandle As Long)
Dim lParam As Long
lParam = MAKELONG(10, 5)
SendMessage lHandle, WM_LBUTTONDOWN, 0, lParam
SendMessage lHandle, WM_LBUTTONUP, 0, lParam
End Sub
Private Function LOWORD(ByVal dw As Long) As Integer
If dw And &H8000& Then LOWORD = dw Or &HFFFF0000 Else LOWORD = dw And &HFFFF&
End Function
Private Function MAKELONG(wLow As Long, wHigh As Long) As Long
MAKELONG = LOWORD(wLow) Or (&H10000 * LOWORD(wHigh))
End Function