如何在C#中为Interop.UIAutomationClient的FindAll()传入True Condition
使用Interop.UIAutomationClient在C#中获取UIAutomation的True Condition
你已经通过Interop.UIAutomationClient成功获取了桌面根元素,现在要调用FindAll枚举桌面子元素,第二个参数的True Condition可以通过你创建的IUIAutomation实例调用CreateTrueCondition()方法来获取,和C++里的用法逻辑完全一致。
完整代码示例
using System; using UIA = Interop.UIAutomationClient; namespace TestUIA { internal class Program { static void Main(string[] args) { Console.WriteLine("Starting..."); UIA.IUIAutomation NativeAutomation = new UIA.CUIAutomation8(); var Desktop = NativeAutomation.GetRootElement(); Console.WriteLine("Desktop name is {0}", Desktop.CurrentName); // 创建匹配所有元素的True Condition var trueCondition = NativeAutomation.CreateTrueCondition(); // 枚举桌面的直接子元素 var desktopChildren = Desktop.FindAll(UIA.TreeScope.TreeScope_Children, trueCondition); Console.WriteLine($"Found {desktopChildren.Length} desktop child elements:"); for (int i = 0; i < desktopChildren.Length; i++) { var element = desktopChildren.GetElement(i); Console.WriteLine($"Element {i+1}: Name = {element.CurrentName}, Control Type = {element.CurrentControlType}"); } Console.WriteLine("Ending..."); } } }
补充说明
CreateTrueCondition()返回的条件会匹配所有UI元素,因此FindAll会返回指定范围内的全部子元素- 若需要枚举桌面下的所有后代元素(包括嵌套层级的元素),可以将第一个参数改为
UIA.TreeScope.TreeScope_Subtree
内容的提问来源于stack exchange,提问作者manuell
相关产品推荐
相关产品推荐

