简体   繁体   中英

C# UI Automation API with multiple virtual desktops on Windows 10

Warning, I'm coming into this problem as someone who had never used C# before a couple of days ago...

I'm trying to write a "simple" program that scrapes text from a targeted window and displays it on a 2x20 VFD display. I've learned about using the Microsoft UI Automation API, and have had some success in using it to accomplish my goal.

However, if the target window is on a different virtual desktop it seems that using TreeWalker on the AutomationElement.RootElement will not find the target.

The code I'm using now to get my target window (while it's on the same virtual desktop):

    public static AutomationElementCollection FindByMultipleConditions(AutomationElement anElement)
    {
        if (anElement == null)
        {
            throw new ArgumentException();
        }
        Condition conditions = new AndCondition(
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane),
            new PropertyCondition(AutomationElement.IsContentElementProperty, true),
            new PropertyCondition(AutomationElement.IsControlElementProperty, true),
            new PropertyCondition(AutomationElement.IsKeyboardFocusableProperty, false),
            new PropertyCondition(AutomationElement.IsEnabledProperty, true),
            new PropertyCondition(AutomationElement.ClassNameProperty, "Chrome_WidgetWin_0")
            );
        AutomationElementCollection elementCollection =
            anElement.FindAll(TreeScope.Children, conditions);
        return elementCollection;
    }

An AutomationElement.RootElement is passed to this method, and this seems to be granular enough to always target the specific window I'm interested in, but it returns nothing if my target window is moved to a different virtual desktop.

Is this a limitation of using the UI Automation API to accomplish my task? Is there a way to iterate through each virtual desktop while searching for my target window or should I try a different way of approaching this?

Thanks!

The managed "System.Windows.Automation" library is old and predates multiple desktops being implemented in Windows as far as I know.

You should try to use the new automation api introduced in Windows 8. Sadly it's not managed, but there is great library called FlaUI that makes it as easy to use as the managed library (be sure to use FlaUI.UIA3 not FlaUI.UIA2 when using it to use the new api).

If you don't want a full library then there is a nuget wrapper for the new com api (from the same guys behind FlaUI I think).

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM