Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function
Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_LBUTTONUP As Long = &H202
Private Const BN_CLICKED As Integer = 245
Sub CommunicateByClickButton(ByVal ApplicationTitle As String)
Dim hwnd As IntPtr = IntPtr.Zero
Dim hwndChild As IntPtr = IntPtr.Zero
hwnd = FindWindow(Nothing, ApplicationTitle)
If hwnd.Equals(IntPtr.Zero) Then
MsgBox("Couldn't find the " & ApplicationTitle)
Exit Sub
Else
AppActivate(ApplicationTitle)
End If
'Get a handle for the button with caption
hwndChild = FindWindowEx(hwnd, 0, "Button", "Yes")
'send BN_CLICKED message
If Not hwndChild.Equals(IntPtr.Zero) Then
SendMessage(CType(hwndChild, Integer), BN_CLICKED, 0, IntPtr.Zero)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CommunicateByClickButton("Sample")
end sub
End Class
Do more with
Imports System.Threading
Imports System.IO
Imports System.ComponentModel
Imports System.Text
Imports System.Runtime.InteropServices
Partial Public Class Form1
Inherits Form
<DllImport("user32")> _
Public Shared Function EnumChildWindows(ByVal window As IntPtr, ByVal callback As EnumWindowProc, ByVal i As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetClassName(ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Private hWnd As IntPtr = FindWindow(Nothing, "Sample")
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function PostMessage(ByVal hWnd As IntPtr, <MarshalAs(UnmanagedType.U4)> ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
End Function
Const WM_click As Integer = 245
Const WM_KEYUP As Integer = &H101
Const WM_CHAR As Integer = &H102
Public Sub New()
InitializeComponent()
End Sub
''' <summary>
''' Returns a list of child windows
''' </summary>
''' <param name="parent">Parent of the windows to return</param>
''' <returns>List of child windows</returns>
Public Shared Function GetChildWindows(ByVal parent As IntPtr) As List(Of IntPtr)
Dim result As New List(Of IntPtr)()
Dim listHandle As GCHandle = GCHandle.Alloc(result)
Try
Dim childProc As New EnumWindowProc(AddressOf EnumWindow)
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle))
Finally
If listHandle.IsAllocated Then
listHandle.Free()
End If
End Try
Return result
End Function
''' <summary>
''' Callback method to be used when enumerating windows.
''' </summary>
''' <param name="handle">Handle of the next window</param>
''' <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
''' <returns>True to continue the enumeration, false to bail</returns>
Private Shared Function EnumWindow(ByVal handle As IntPtr, ByVal pointer As IntPtr) As Boolean
Dim gch As GCHandle = GCHandle.FromIntPtr(pointer)
Dim list As List(Of IntPtr) = TryCast(gch.Target, List(Of IntPtr))
If list Is Nothing Then
Throw New InvalidCastException("GCHandle Target could not be cast as List<IntPtr>")
End If
list.Add(handle)
' You can modify this to check to see if you want to cancel the operation, then return a null here
Return True
End Function
''' <summary>
''' Delegate for the EnumChildWindows method
''' </summary>
''' <param name="hWnd">Window handle</param>
''' <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
''' <returns>True to continue enumerating, false to bail.</returns>
Public Delegate Function EnumWindowProc(ByVal hWnd As IntPtr, ByVal parameter As IntPtr) As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each child As IntPtr In GetChildWindows(FindWindow(Nothing, "Sample"))
Dim sb As New StringBuilder(100)
GetClassName(child, sb, sb.Capacity)
If sb.ToString.Contains("BUTTON") Then
Dim wparam As UInteger = 0 << 29 Or 0
PostMessage(child, WM_click, 0, IntPtr.Zero)
End If
Next
End Sub
End Class
Premium Content
You need an Expert Office subscription to comment.Start Free Trial