/// <summary> /// Creates a new instance of <see cref="Window"/>. /// </summary> /// <param name="handle">The Window Handle.</param> public Window(IntPtr handle) { if (!User32.IsWindow(handle)) { throw new ArgumentException("Not a Window.", nameof(handle)); } Handle = handle; }
/// <summary> /// Enumerates all Windows. /// </summary> public static IEnumerable <Window> Enumerate() { var list = new List <Window>(); User32.EnumWindows((handle, param) => { var wh = new Window(handle); list.Add(wh); return(true); }, IntPtr.Zero); return(list); }
/// <summary> /// Enumerates all visible windows with a Title. /// </summary> public static IEnumerable <Window> EnumerateVisible() { foreach (var window in Enumerate().Where(window => window.IsVisible && !string.IsNullOrWhiteSpace(window.Title))) { var hWnd = window.Handle; if (!User32.GetWindowLong(hWnd, GetWindowLongValue.ExStyle).HasFlag(WindowStyles.AppWindow)) { if (User32.GetWindow(hWnd, GetWindowEnum.Owner) != IntPtr.Zero) { continue; } if (User32.GetWindowLong(hWnd, GetWindowLongValue.ExStyle).HasFlag(WindowStyles.ToolWindow)) { continue; } if (User32.GetWindowLong(hWnd, GetWindowLongValue.Style).HasFlag(WindowStyles.Child)) { continue; } } const int dwmCloaked = 14; // Exclude suspended Windows apps DwmApi.DwmGetWindowAttribute(hWnd, dwmCloaked, out var cloaked, Marshal.SizeOf <bool>()); if (cloaked) { continue; } yield return(window); } }