按键精灵9插件:EnumChilds 遍历符合窗口



在上图所示的软件中,StandardWindow下面有3个ToolBarPlus,当要获取句柄为10488138的ToolBarPlus时,尝试了下面几个命令均不成功:

Plugin.Window.FindEx
Plugin.Window.Search
Plugin.Window.SearchEx

没办法,只能写插件来调用winapi的EnumChildWindows了。

按键精灵9插件:EnumChilds 遍历符合窗口

解压并用VB6.0打开按键精灵9自带的QMPlugin插件制作模版(VB 6.0),增加一个模块:

Option Explicit

Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private hWnds As String

'--------------------------------------------------------------------------------
'* 枚举子窗口回调函数
'*
'* @param    Long      hWnd         子窗口句柄
'* @param    Integer     lParam     调用 EnumChildWindows 函数时传递的参数
'* @return   Boolean
'--------------------------------------------------------------------------------
Private Function EnumFunc(ByVal hWnd As Long, ByVal lParam As Integer) As Boolean
    hWnds = hWnds & CStr(hWnd) & "|"
    EnumFunc = True
End Function

'--------------------------------------------------------------------------------
'* 获取指定句柄的窗口类名
'*
'* @param    Long      hWnd    窗口句柄
'* @return   String
'--------------------------------------------------------------------------------
Private Function GetClassNameX(ByVal hWnd As Long) As String
    Dim cName As String, nLength As Integer
    nLength = 255
    cName = Space(nLength)
    GetClassName hWnd, cName, nLength
    GetClassNameX = cName
End Function

'--------------------------------------------------------------------------------
'* 获取子窗口句柄
'*
'* @param    Long      hWnd             父窗口句柄
'* @param    String    lpClassName      子窗体类名
'* @return   String                     窗体1句柄|窗体2句柄|窗体3句柄
'--------------------------------------------------------------------------------
Public Function EnumChildWindowsX(ByVal hWnd As Long, ByVal lpClassName As String) As String
    Dim LstHwnd, i
    hWnds = ""
    EnumChildWindows hWnd, AddressOf EnumFunc, 0
    '条件筛选
    If Len(hWnds) > 0 And lpClassName <> vbNullString Then
        LstHwnd = Split(hWnds, "|")
        hWnds = ""
        For i = 0 To UBound(LstHwnd)
            If LstHwnd(i) <> "" Then
                If LCase(Mid(GetClassNameX(CLng(LstHwnd(i))), 1, Len(lpClassName))) = LCase(lpClassName) Then
                    hWnds = hWnds & CStr(LstHwnd(i)) & "|"
                End If
            End If
        Next
    End If
    '函数返回值
    If Len(hWnds) > 0 Then
        EnumChildWindowsX = Mid(hWnds, 1, Len(hWnds) - 1)
    Else
        EnumChildWindowsX = ""
    End If
End Function

修改类模块:

Public Function EnumChilds(hWnd As Variant, lpClassName As String) As String
    EnumChilds = EnumChildWindowsX(CLng(hWnd), lpClassName)
End Function


Public Function Get_Plugin_Description(ItemName As String) As String
    Dim Description_Text As String
    Description_Text = ""
    Select Case ItemName
    Case ""
        Description_Text = "窗口扩展插件"
    Case "EnumChilds"
        Description_Text = "遍历符合窗口"
    End Select
    Get_Plugin_Description = Translate_Description(Description_Text)
End Function

Public Function Get_Plugin_Interpret_Template(ItemName As String) As String
    Dim Description_Text As String
    Description_Text = ""
    Select Case ItemName
    Case ""
        Description_Text = "窗口扩展插件"
    Case "EnumChilds"
        Description_Text = "遍历符合窗口"
    End Select
    Get_Plugin_Interpret_Template = Translate_Description(Description_Text)
End Function

生成WindowEx.dll,放到按键精灵安装目录的plugin文件夹中就可以使用了:



调用语法

Plugin.WindowEx.EnumChilds(hwnd, vbNullString) //获取全部子窗口句柄
Plugin.WindowEx.EnumChilds(hwnd, "ToolBarPlus") //获取指定类名子窗口句柄

也可以直接下载编译好的WindowEx.dll:点击下载

评论: 0 | 引用: 0 | 查看次数: 7359
发表评论
登录后再发表评论!