private static extern bool GetTitleBarInfo(IntPtr hWnd, ref TitlebarInfo info);
/// <summary> /// Returns a dictionary that contains the handle and title of all the open windows. /// </summary> /// <returns> /// A dictionary that contains the handle and title of all the open windows. /// </returns> public static List <DetectedRegion> EnumerateWindows(double scale = 1) { var shellWindow = User32.GetShellWindow(); var windows = new List <DetectedRegion>(); //EnumWindows(delegate (IntPtr handle, int lParam) User32.EnumDesktopWindows(IntPtr.Zero, delegate(IntPtr handle, IntPtr lParam) { if (handle == shellWindow) { return(true); } if (!User32.IsWindowVisible(handle)) { return(true); } if (User32.IsIconic(handle)) { return(true); } var length = User32.GetWindowTextLength(handle); if (length == 0) { return(true); } var builder = new StringBuilder(length); User32.GetWindowText(handle, builder, length + 1); var info = new WindowInfo(false); User32.GetWindowInfo(handle, ref info); //If disabled, ignore. if (((long)info.dwStyle & (uint)WindowStyles.Disabled) == (uint)WindowStyles.Disabled) { return(true); } //Window class name. var className = new StringBuilder(256); //Maximum class name. if (User32.GetClassName(handle, className, className.Capacity) != 0) { if (className.ToString().Contains("ScreenToGif.exe")) { return(true); } } //Title bar visibility. var infoTile = new TitlebarInfo(false); User32.GetTitleBarInfo(handle, ref infoTile); //Removed: WindowStyle=None windows were getting ignored. // ((infoTile.rgstate[0] & StateSystemInvisible) == StateSystemInvisible) // return true; if ((infoTile.rgstate[0] & Constants.StateSystemUnavailable) == Constants.StateSystemUnavailable) { return(true); } ////Removed: MahApps windows were getting ignored. //if ((infoTile.rgstate[0] & StateSystemOffscreen) == StateSystemOffscreen) // return true; DwmApi.DwmGetWindowAttribute(handle, (int)DwmWindowAttributes.Cloaked, out bool isCloacked, Marshal.SizeOf(typeof(bool))); if (isCloacked) { return(true); } DwmApi.DwmGetWindowAttribute(handle, (int)DwmWindowAttributes.ExtendedFrameBounds, out NativeRect frameBounds, Marshal.SizeOf(typeof(NativeRect))); var bounds = frameBounds.TryToRect(MathExtensions.RoundUpValue(scale), scale); if (bounds.IsEmpty) { return(true); } windows.Add(new DetectedRegion(handle, bounds, builder.ToString(), GetZOrder(handle))); return(true); }, IntPtr.Zero); return(windows.OrderBy(o => o.Order).ToList()); }
internal static extern bool GetTitleBarInfo(IntPtr hwnd, ref TitlebarInfo pti);
/// <summary> /// Returns a dictionary that contains the handle and title of all the open windows inside a given monitor. /// </summary> /// <returns> /// A dictionary that contains the handle and title of all the open windows. /// </returns> public static List <DetectedRegion> EnumerateWindowsByMonitor(Monitor monitor) { var shellWindow = User32.GetShellWindow(); var windows = new List <DetectedRegion>(); //EnumWindows(delegate (IntPtr handle, int lParam) User32.EnumDesktopWindows(IntPtr.Zero, delegate(IntPtr handle, IntPtr lParam) { if (handle == shellWindow) { return(true); } if (!User32.IsWindowVisible(handle)) { return(true); } if (User32.IsIconic(handle)) { return(true); } var length = User32.GetWindowTextLength(handle); if (length == 0) { return(true); } var builder = new StringBuilder(length); User32.GetWindowText(handle, builder, length + 1); var title = builder.ToString(); var info = new WindowInfo(false); User32.GetWindowInfo(handle, ref info); //If disabled, ignore. if (((long)info.dwStyle & (uint)WindowStyles.Disabled) == (uint)WindowStyles.Disabled) { return(true); } //Window class name. var className = new StringBuilder(256); //Maximum class name. if (User32.GetClassName(handle, className, className.Capacity) != 0) { if (className.ToString().Contains("ScreenToGif.exe")) { return(true); } } var infoTile = new TitlebarInfo(false); User32.GetTitleBarInfo(handle, ref infoTile); //Removed: WindowStyle=None windows were getting ignored. // ((infoTile.rgstate[0] & StateSystemInvisible) == StateSystemInvisible) // return true; if ((infoTile.rgstate[0] & Constants.StateSystemUnavailable) == Constants.StateSystemUnavailable) { return(true); } ////Removed: MahApps windows were getting ignored. //if ((infoTile.rgstate[0] & StateSystemOffscreen) == StateSystemOffscreen) // return true; DwmApi.DwmGetWindowAttribute(handle, (int)DwmWindowAttributes.Cloaked, out bool isCloacked, Marshal.SizeOf(typeof(bool))); if (isCloacked) { return(true); } DwmApi.DwmGetWindowAttribute(handle, (int)DwmWindowAttributes.ExtendedFrameBounds, out NativeRect frameBounds, Marshal.SizeOf(typeof(NativeRect))); var bounds = frameBounds.TryToRect(MathExtensions.RoundUpValue(monitor.Scale), monitor.Scale); if (bounds.IsEmpty) { return(true); } var place = WindowPlacement.Default; User32.GetWindowPlacement(handle, ref place); //Hack for detecting the correct size of VisualStudio when it's maximized. if (place.ShowCmd == ShowWindowCommands.Maximize && title.Contains("Microsoft Visual Studio")) { bounds = frameBounds.TryToRect(-info.cxWindowBorders, monitor.Scale); } //bounds = new System.Windows.Rect(new Point(monitor.Bounds.Left / monitor.Scale, monitor.Bounds.Top / monitor.Scale), new Size(info.rcClient.Right / monitor.Scale, info.rcClient.Bottom / monitor.Scale)); if (bounds.IsEmpty) { return(true); } //Windows to the left are not being detected as inside the bounds. if (!bounds.IntersectsWith(monitor.Bounds)) { return(true); } windows.Add(new DetectedRegion(handle, bounds, title, GetZOrder(handle))); return(true); }, IntPtr.Zero); return(windows.OrderBy(o => o.Order).ToList()); }