EnumChildWindows枚举所有窗口-2

Imports System.Runtime.InteropServices
Imports System.Text



 '调查窗口标题文字或控件内容的长短(在vb里使用:直接使用vb窗体或控件的caption或text属性)
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Integer
    '取得一个窗体的标题(caption)文字,或者一个控件的内容(在vb里使用:使用vb窗体或控件的caption或text属性)
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
    '为指定的窗口取得类名
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
    '为指定的父窗口枚举子窗口
    Private Declare Function EnumChildWindows Lib "user32" Alias "EnumChildWindows" (ByVal hWndParent As System.IntPtr, ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean
    Private Delegate Function EnumWindowsProc(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
  

#Region "模糊搜索与精确搜索"

 ''' SearchWin枚举函数中回调函数的委托函数 
    Public Function SearchWinCallBack(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
        Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
        If ChildrenList Is Nothing Then Throw New Exception("GCHandle Target could not be cast as List(Of IntPtr)")
        ChildrenList.Add(Handle)
        Return True
    End Function


    ''' 
    ''' 枚举所有符合类名,标题名的窗口句柄,;父句柄为0表示桌面,类名\标题为空时用""表示。
    ''' 
    ''' 要查找窗口的父窗口句柄,如果是桌面用0
    ''' 被找窗口的类名
    ''' 被找窗口的标题
    ''' 带|隔开的字符串
    ''' 
    Public Function SearchWin(ByVal ParentHandle As IntPtr, ByVal ClassName As String, ByVal StrTitle As String) As String
        Dim ChildrenList As New List(Of IntPtr)
        Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
        Dim sbClassName As New System.Text.StringBuilder("", 256) '设置类名的文本类型和长度
        Dim texthwnd As String = Nothing
        Try
            EnumChildWindows(ParentHandle, AddressOf SearchWinCallBack, GCHandle.ToIntPtr(ListHandle))
            '循环所有找到的句柄
            For Each i In ChildrenList
                Dim length As Integer = GetWindowTextLength(i) '得到标题文本的长度
                Dim sbStrTitle As New System.Text.StringBuilder("", length) '设置窗口标题的文本类型和长度
                GetClassName(i, sbClassName, 256) '得到窗口类名
                GetWindowText(i, sbStrTitle, sbStrTitle.Capacity + 1) '得到窗口标题
                Debug.WriteLine(sbClassName.ToString & "-----" & sbStrTitle.ToString)
                If sbClassName.ToString.Contains(ClassName) And sbStrTitle.ToString.Contains(StrTitle) Then     ' 返回一个值,该值指示指定的System.String对象是否出现在此字符串中。
                    texthwnd += i.ToString & "|" ' 存储该句柄
                    MsgBox(texthwnd)
                End If
            Next
        Catch ex As Exception
            If ListHandle.IsAllocated Then ListHandle.Free()
        End Try
        Return texthwnd
        'Return ChildrenList.ToArray
    End Function

#End Region

 

你可能感兴趣的:(VB编程,窗口句柄搜索,枚举所有窗口)