VB6 UIAutomation 自动化查找元素

UI Automation 的 FindFirst 只会返回第一个匹配的元素 ,如果你有多个符合条件的 "窗格",就需要用 FindAll 来获取所有匹配项,然后选择第 2 个,再在它下面继续查找子元素。


1. 基本思路

  1. 找顶层窗口bfMain
  2. FindAll 获取所有窗格
  3. 取第 2 个窗格(索引为 1,因为集合是 0 起始)
  4. 在第 2 个窗格中找子元素 (例如 toolStrip

2. VB6 示例代码

vbnet 复制代码
Option Explicit

Private Sub Command1_Click()
    Dim UIA As CUIAutomation
    Dim root As IUIAutomationElement
    Dim topElement As IUIAutomationElement
    Dim paneElements As IUIAutomationElementArray
    Dim paneElement As IUIAutomationElement
    Dim toolStripElement As IUIAutomationElement
    Dim paneCount As Long
    
    Set UIA = New CUIAutomation
    Set root = UIA.GetRootElement
    
    ' 1. 找顶层窗口
    Set topElement = root.FindFirst( _
        TreeScope_Descendants, _
        UIA.CreatePropertyCondition(UIA_AutomationIdPropertyId, "bfMain"))
    
    If Not topElement Is Nothing Then
        ' 2. 找所有窗格
        Set paneElements = topElement.FindAll( _
            TreeScope_Descendants, _
            UIA.CreatePropertyCondition(UIA_LocalizedControlTypePropertyId, "窗格"))
        
        paneCount = paneElements.Length
        If paneCount >= 2 Then
            ' 3. 取第 2 个窗格(索引 1)
            Set paneElement = paneElements.GetElement(1)
            
            ' 4. 在第 2 个窗格中找 toolStrip
            Set toolStripElement = paneElement.FindFirst( _
                TreeScope_Descendants, _
                UIA.CreatePropertyCondition(UIA_NamePropertyId, "toolStrip"))
            
            If Not toolStripElement Is Nothing Then
                MsgBox "找到第 2 个窗格下的 toolStrip: " & toolStripElement.CurrentName
            Else
                MsgBox "第 2 个窗格下未找到 toolStrip"
            End If
        Else
            MsgBox "找到的窗格数量不足 2 个"
        End If
    Else
        MsgBox "未找到 bfMain"
    End If
End Sub

3. 关键点说明

  • FindAll 返回 IUIAutomationElementArray,包含所有匹配条件的元素。

  • 索引 集合索引是 0 起始 ,所以第 2 个窗格的索引是 1

  • **TreeScope_Descendants**搜索范围是所有子孙元素,包括子控件、孙控件等。

  • 多条件匹配 如果 "窗格" 还需要额外条件(比如 Name),可以用 CreateAndConditionFromArray 组合条件。


4. 如果窗格数量可能很多

你可以先遍历所有窗格,输出它们的 CurrentNameCurrentAutomationId,这样方便确定哪一个是你要的第 2 个。

示例:

复制代码
Dim i As Long
For i = 0 To paneCount - 1
    Debug.Print i & ": " & paneElements.GetElement(i).CurrentName
Next

✅ 总结:

  • FindAll 获取所有窗格
  • 用索引选择第 2 个窗格
  • 再在该窗格下用 FindFirstFindAll 查找子元素

如果你愿意,我可以帮你写一个 通用的按层级查找函数,可以一次传入多个条件数组,自动定位到第 N 个元素,然后继续找它的子元素。这样你查找复杂界面会非常方便。

你要我帮你做这个通用查找函数吗?这样你以后查找第 2 个窗格的子元素就可以一行代码搞定。

相关推荐
宴之敖者、14 小时前
Linux——权限
linux·运维·服务器
oscar99915 小时前
构建敏捷团队的DevOps测试策略:速度与可靠性的平衡艺术
运维·测试·devops
星辰&与海16 小时前
Proxmox导入虚拟机后进入dracut紧急模式
运维
Serverless社区16 小时前
进阶指南:BrowserUse + Agentrun Sandbox 最佳实践指南
运维·阿里云·云原生·serverless·函数计算
Maggie_ssss_supp16 小时前
Linux-MySQL权限管理
linux·运维·mysql
石像鬼₧魂石16 小时前
Kali Linux 内网渗透:深度工程实施手册
linux·运维·服务器
HXDGCL17 小时前
大会观察 | 破除创新链堵点:论“工厂直供”模式如何加速自动化核心部件迭代
大数据·人工智能·自动化·自动化生产线·环形导轨
sunfove17 小时前
Python 自动化实战:从识图点击、模拟真人轨迹到封装 EXE 全流程教学
开发语言·python·自动化
傻啦嘿哟17 小时前
Python网页自动化操作全攻略:从入门到实战
开发语言·python·自动化
HXDGCL17 小时前
从2025中国自动化大会看核心部件创新:立式环形导轨单板双轨道如何支撑“智能”与“协同”?
自动化·自动化生产线·环形导轨