/// <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)); } this.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(W => W.IsVisible && !string.IsNullOrWhiteSpace(W.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); } }