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 "模糊搜索与精确搜索"

 ''' <summary>SearchWin枚举函数中回调函数的委托函数 </summary>
    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


    ''' <summary>
    ''' 枚举所有符合类名,标题名的窗口句柄,;父句柄为0表示桌面,类名\标题为空时用""表示。
    ''' </summary>
    ''' <param name="ParentHandle">要查找窗口的父窗口句柄,如果是桌面用0</param>
    ''' <param name="ClassName">被找窗口的类名</param>
    ''' <param name="StrTitle">被找窗口的标题</param>
    ''' <returns>带|隔开的字符串</returns>
    ''' <remarks></remarks>
    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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值