question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get Window object for apps with more than one "main" window

See original GitHub issue

Describe the bug For example, Google Chrome can have multiple open windows, but FlaUI only allows getting the automation controls for the “main” one, which is arbitrary. It’d be great if there was either a GetMainWindows method, or just a FromHandle method like AutomationElement has.

Code snippets

var app = FlaUI.Core.Application.Attach("chrome.exe");
    using (var automation = new UIA3Automation())
    {
        var window = app.GetMainWindow(automation);

Currently, I can’t use the library, I instead have to enumerate the windows manually (I use the https://github.com/zastrowm/Win32Interop.WinHandles library for this), and use the built-in .NET automation library:

foreach (var window in EnumerateWindows(chromeProcess))
{
    AutomationElement root = AutomationElement.FromHandle(window);
    ...
}

static IEnumerable<IntPtr> EnumerateWindows(Process process)
{
    return TopLevelWindowUtils.FindWindows(w => w.IsVisible())
        .Where(w =>
        {
            PInvoke.GetWindowThreadProcessId(w.RawPtr, out uint processId);
            return processId == process.Id;
        })
        .Select(w => w.RawPtr);
}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ebram-hcommented, Jan 2, 2023

One thing to consider with GetAllTopLevelWindows is that it returns only top-level windows, like the main window, not its child windows. To get the windows that are opened by the main window try:

mainWindow.FindAllChildren(w => w.ByControlType(FlaUI.Core.Definitions.ControlType.Window));
1reaction
palenshuscommented, Sep 28, 2022

I get different results with that. I tried it using Chrome with two windows open:

using var app = FlaUI.Core.Application.Attach("chrome.exe");

using var automation = new UIA3Automation();

var topLevelWindows = app.GetAllTopLevelWindows(automation);
topLevelWindows.Count().Dump();    // returns 0

var visibleWindows = automation.GetDesktop().FindAllChildren(e => e.ByProcessId(app.ProcessId));
visibleWindows.Count().Dump();    // returns 2
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to share the same window object with the other web ...
The problem is that the new page is creating its own window object and cannot access the properties/methods of the first page's window...
Read more >
Show multiple views with AppWindow
Use the AppWindow class to view different parts of your app in separate windows.
Read more >
Multi-window support
Quickly bring your app to life with less code, using a modern declarative approach to UI, and the simplicity of Kotlin. ... Start...
Read more >
How to Handle Multiple Windows in an Electron App
In Electron, each window corresponds to a renderer process. Here's a way of setting up the logic of handling multiple windows in the...
Read more >
Multiple windows in PyQt5
In Qt any widget without a parent is a window. This means, to show a new window you just need to create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found