문제

I want to build a tree of UI elements that are present in an application. I am trying to use Microsoft UI Automation. I can get access to the desktop by accessing AutomationElement.RootElement but I am not sure how to access the application that is running on the desktop.

The main goal is to build a UI tree of the target application already running on the desktop. I do not have access to the code of the target application.

When I try to run

foreach (var item in AutomationElement.RootElement.CachedChildren)
        {
            Console.WriteLine(item.ToString());
        }

It gives me Fatal error: Can not get the property or element that is not cached.

Please suggest what is the best way to tackle this problem.

도움이 되었습니까?

해결책

You're trying to use UIA's caching feature, which allows you to cache AutomationElements and their properties and improve performance upon accessing them. This is an advanced feature obviously unrelated to your question, as you'll first need to actually get the AutomationElements from the UI tree.

Also, your code, even if it worked, wouldn't have returned all the tree, just the child elements of the desktop (in UIA, "child elements" actually mean elements found just one hierarchy under the particular element). In order to navigate on the UI tree, you'll want to use the TreeWalker class. There's fairly good documentation on it on MSDN, so I suggest you go there first before you start using it.

Note that if you want to map your own application and not the entire UI tree, you'll need to instantiate the TreeWalker object with a Condition that will return your application's main window.

One last note - calling ToString() on an AutomationElement wouldn't return anything of particular interest. If you want to get the name of the control or any other property, you'll want to use the GetCurrentPropertyValue method (or access Current.{PropertyName}).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top