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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您是在Windows操作系统下进行开发,可以使用以下方法来枚举所有窗口: 1. 使用EnumWindows函数来枚举所有顶级窗口。该函数的原型为: ```c++ BOOL EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam ); ``` 其中,lpEnumFunc是一个回调函数,用于处理枚举到的每个窗口,lParam是一个用户参数,可以传递给回调函数。 2. 在回调函数中,可以使用GetWindowText函数来获取窗口标题,使用GetClassName函数来获取窗口类名,使用IsWindowVisible函数来判断窗口是否可见。 3. 如果要获取子窗口,可以使用EnumChildWindows函数来枚举指定父窗口的所有子窗口。该函数的原型为: ```c++ BOOL EnumChildWindows( HWND hWndParent, WNDENUMPROC lpEnumFunc, LPARAM lParam ); ``` 其中,hWndParent是父窗口句柄,lpEnumFunc和lParam的含义与EnumWindows函数相同。 下面是一个示例代码,可以枚举所有顶级窗口,并输出每个窗口的标题和类名: ```c++ #include <Windows.h> #include <iostream> using namespace std; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { char title[256]; char className[256]; GetWindowText(hwnd, title, sizeof(title)); GetClassName(hwnd, className, sizeof(className)); if (IsWindowVisible(hwnd)) { cout << "Title: " << title << endl; cout << "Class: " << className << endl; cout << "-----------------------" << endl; } EnumChildWindows(hwnd, EnumWindowsProc, lParam); return TRUE; } int main() { EnumWindows(EnumWindowsProc, NULL); return 0; } ``` 该示例代码会输出所有可见窗口的标题和类名,包括子窗口。您可以根据需要进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值