我正在尝试通过UIAutomationCore.dll库进行特定控制。问题是,我无法从代码中看到它(找不到所需的子节点)和UISpy(它显示异常“UI自动化树导航被破坏。其中一个后代的父节点存在,但后代不是父节点的子节点“)直到我启动inspect.exe甚至一秒钟 - 然后它出现,这个效果一直持续到程序窗口重新打开。
有类似的question,但应用程序运行在同一个非管理员级别的UAC上。
这是我的代码。这是VS Code选项卡的一个示例,但我也遇到了Slack及其“文档”的问题。
static bool Do(IntPtr handle)
{
CUIAutomation8 uiAutomation = new CUIAutomation8();
int propertyName = 30005;
int propertyLocalizedType = 30004;
IUIAutomationElement element = uiAutomation.ElementFromHandle(handle);
if (element == null)
return false;
IUIAutomationCacheRequest cacheRequest = uiAutomation.CreateCacheRequest();
cacheRequest.AddProperty(propertyName);
IUIAutomationCondition tabCondition = uiAutomation.CreatePropertyCondition(propertyLocalizedType, "tab");
IUIAutomationElementArray elements = element.FindAllBuildCache(
TreeScope.TreeScope_Descendants,
tabCondition,
cacheRequest);
if (elements == null || elements.Length == 0)
return false;
for (int i = 0; i < elements.Length; ++i)
{
//do something
}
return true;
}
我有一个非常相似的经历 - 在inspect.exe短暂运行之前,某些应用程序的UI树是不可见的。我最终发现将注册表项HKCU \ Control Panel \ Accessibility \ Blind Access \ On设置为“1”使其行为正常。真正有趣的部分是: