VB.NET实现的控制外部应用程序窗口或控件行为的函数源程序

与大家分享几个VB.NET实现的控制外部应用程序窗口或控件行为的函数源程序代码。
其中包括时间延迟的处理,窗口类或窗口名称的获得可以利用Visual Studio Tools中的Spy++来得到。
函数的设计主要是利用Windows API 的两个函数:
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hwnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
下面是一个在C的API编程中经常要使用的宏
    Public Function MakeDWORD(ByVal loWord As Integer, ByVal hiWord As Integer) As Integer MakeDWORD = (hiWord * &H10000) Or (loWord And &HFFFF&) End Function
这个函数按窗口名称或窗口类名,查找指定窗口的句柄
    Public Function myFindWindow(ByVal wndName As String, ByVal className As String, ByVal delayTime As Integer) As Integer Dim i As Integer Dim hwnd As Integer hwnd = 0 For i = 0 To delayTime / 200 hwnd = FindWindow(className, wndName) If hwnd > 0 Then Exit For End If System.Threading.Thread.CurrentThread.Sleep(200) Application.DoEvents() Next Return hwnd End Function
这个函数按窗口名称或窗口类名数组,并沿着父子关系,查找指定窗口的句柄
    Public Function myFindChildWindow(ByVal parentHwnd As Integer, ByVal wndNames() As String, ByVal classNames() As String, ByVal ub As Integer, ByVal delayTime As Integer) As Integer Dim i, j As Integer Dim hwnd As Integer Dim cldhwnd As Integer cldhwnd = 0 hwnd = parentHwnd For j = 0 To ub - 1 For i = 0 To delayTime / 200 cldhwnd = FindWindowEx(hwnd, 0, classNames(j), wndNames(j)) If cldhwnd > 0 Then hwnd = cldhwnd i = delayTime + 1 Else Application.DoEvents() System.Threading.Thread.CurrentThread.Sleep(200) End If Next If cldhwnd = 0 Then Return 0 End If Next Return cldhwnd End Function
这个函数按窗口名称或窗口类名数组,并沿着兄弟关系,查找指定窗口的句柄
    Public Function myFindBrotherWindow(ByVal parentHwnd As Integer, ByVal index As Integer, ByVal wndName As String, ByVal className As String, ByVal delayTime As Integer) As Integer Dim i, j As Integer Dim brotherhwnd As Integer Dim hwnd As Integer hwnd = 0 brotherhwnd = 0 For j = 1 To index For i = 0 To delayTime / 200 hwnd = FindWindowEx(parentHwnd, brotherhwnd, className, wndName) If hwnd > 0 Then brotherhwnd = hwnd i = delayTime + 1 Else Application.DoEvents() System.Threading.Thread.CurrentThread.Sleep(200) End If Next Next Return hwnd End Function

你可能感兴趣的:(VB.NET实现的控制外部应用程序窗口或控件行为的函数源程序)