I have a simple application consisting of a form and two buttons, both of which do nothing more than close the form. I'm trying to make another application that will close the form. It seems to obtain the window and button handles fine, but cannot seem to click them. The code I have so far is listed below. If anyone has any idea why it's not working, I'd appreciate it. "Joe" is the name of the other Program, and "Exit" is the caption of the button I want to press. Const GW_CHILD As Integer = 5
Const GW_HWNDNEXT As Integer = 2
Const WM_GETTEXT As Integer = &HD
Const WM_GETTEXTLENGTH As Integer = &HE
Const BM_SETSTATE As Integer = &HF3
Const WM_LBUTTONUP As Integer = &H202
Const WM_LBUTTONDOWN As Integer = &H201
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Auto Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Integer) As IntPtr
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByRef lParam As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, _
ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr
Public Function GetWindows(ByVal ParentWindowHandle As IntPtr) As IntPtr()
Dim ptrChild As IntPtr
Dim ptrRet() As IntPtr
Dim iCounter As Integer
'get first child handle...
ptrChild = GetWindow(ParentWindowHandle, GW_CHILD)
'loop through and collect all child window handles...
Do Until ptrChild.Equals(IntPtr.Zero)
'process child...
ReDim Preserve ptrRet(iCounter)
ptrRet(iCounter) = ptrChild
'get next child...
ptrChild = GetWindow(ptrChild, GW_HWNDNEXT)
iCounter += 1
Loop
'return...
Return ptrRet
End Function
Public Function GetWindowText(ByVal WindowHandle As IntPtr) As String
Dim ptrRet As IntPtr
Dim ptrLength As IntPtr
'get length for buffer...
ptrLength = SendMessage(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
'create buffer for return value...
Dim sbText As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)
'get window text...
ptrRet = SendMessage(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sbText)
'get return value...
Return sbText.ToString
End Function
Public Sub ClickButton(ByVal ButtonHandle As IntPtr)
'send the left mouse button "down" message to the button...
Call SendMessage(ButtonHandle, WM_LBUTTONDOWN, 0, IntPtr.Zero)
'send the left mouse button "up" message to the button...
Call SendMessage(ButtonHandle, WM_LBUTTONUP, 0, IntPtr.Zero)
'send the button state message to the button, telling it to handle its events...
Call SendMessage(ButtonHandle, BM_SETSTATE, 1, IntPtr.Zero)
End Sub
Private Sub btnPress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPress.Click
Dim hwnd As IntPtr
hwnd = FindWindow(vbNullString, "Joe")
Dim ptrChildWindows() As IntPtr = GetWindows(hwnd)
MsgBox(ptrChildWindows.Length)
For iCounter As Integer = 0 To ptrChildWindows.Length - 1
'grab the current handle to process...
Dim ptrCurrent As IntPtr = ptrChildWindows(iCounter)
'get the window text...
Dim sText As String = GetWindowText(ptrCurrent)
MsgBox(sText)
'check to see if this is the button we are looking for...
If sText = "Exit" Then
'click the button to close the dialog...
ClickButton(ptrCurrent)
'done deal...
Exit For
Else
MsgBox("Not Clicked")
End If
Next
End Sub