Finding all windows on desktop using UIAutomation .NET
Asked Answered
P

3

9

I am trying to find all desktop windows using .NET UIAutomation OR White framework. I tried :

1.)

            AutomationElement rootElement = AutomationElement.RootElement;
            var winCollection = rootElement.FindAll(TreeScope.Subtree, Condition.TrueCondition);

2.)

            Desktop.Instance.Windows();

Both throw ArgumentException. Please let me know if there are other ways to do this...

UPDATE/ANSWER: Desktop.Instance.Windows(); works fine except that it throws exception while debugging the code using VS2010.

Psychiatry answered 17/4, 2012 at 12:27 Comment(0)
P
8

Using TreeScope.Children should work if you want to access the immediate child elements of the desktop ::

    AutomationElement rootElement = AutomationElement.RootElement;
    var winCollection = rootElement.FindAll(TreeScope.Children, Condition.TrueCondition);

    foreach (AutomationElement element in winCollection)
        Console.WriteLine(element.Current.Name);
Piecrust answered 17/4, 2012 at 20:54 Comment(0)
E
2

a UI automation project at work uses:

List<White.Core.UIItems.WindowItems.Window> windows = WindowFactory.Desktop.DesktopWindows();
Engagement answered 1/5, 2012 at 20:27 Comment(0)
A
0

It's impossible by means of the FindAll call. Some types of windows are available only by calling FindWindow. These may be a child window of MMC console, at the time when the main window of mmc is hidden and unavailable to UI Automation. Is there a way to get a thread the window is on, in case the window is not the main window of the application? The answer is to enumerate threads (often, administrative rights are needed) and calculate which is the thread the window of our interest belongs to.

Generally, there seems no way available to enumerate all the windows as it does Spy++. For example, Get-UIAWindow simply calls FindWindow if UI Automation search was fruitless.

Ambidexterity answered 19/4, 2012 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.