VB.NET 寻找微信窗体所有的按钮组件

VB.NET · 2023-06-24 · 280 人浏览

Imports System.Windows.Automation
Imports System.Runtime.InteropServices
Public Class Form1

<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    GetChatWndElements()
End Sub
Public Sub GetChatWndElements()
    ' 根据快速窗口类名查找窗口
    Dim text As String
    Dim condition As Condition = New PropertyCondition(AutomationElement.ClassNameProperty, "ChatWnd")
    Dim chatWnd As AutomationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, condition)

    If chatWnd IsNot Nothing Then
        ' 查找所有子元素
        Dim elementCollection As AutomationElementCollection = chatWnd.FindAll(TreeScope.Descendants, New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button))

        ' 遍历子元素并输出名称
        For Each element As AutomationElement In elementCollection
            Dim name As String = element.Current.Name
            If Not String.IsNullOrEmpty(name) Then
                text = text + name + vbCrLf
            End If
        Next
        TextBox1.Text = text
    Else
        Console.WriteLine("未找到名为 ChatWnd 的窗口。")
    End If
End Sub

End Class

VB.NET