using System.Runtime.InteropServices;
namespace Febe.Tests.Windows.Support;
///
/// The direct child windows of a window, by class name. WebView2 hosted in a window («finestra») puts one child window
/// per controller into its parent: two of them in «Browser di Febe» mean two WebViews, one of them orphaned.
///
internal static class ChildWindows
{
/// The class names of the direct children of , in Z order.
public static IReadOnlyList Of(IntPtr parent)
{
var names = new List();
var buffer = new char[256];
var child = IntPtr.Zero;
while ((child = FindWindowEx(parent, child, null, null)) != IntPtr.Zero)
{
var length = GetClassName(child, buffer, buffer.Length);
names.Add(new string(buffer, 0, Math.Max(length, 0)));
}
return names;
}
[DllImport("user32.dll", EntryPoint = "FindWindowExW", CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string? className, string? windowName);
[DllImport("user32.dll", EntryPoint = "GetClassNameW", CharSet = CharSet.Unicode)]
private static extern int GetClassName(IntPtr window, [Out] char[] className, int maxCount);
}