Beispiel #1
0
        public static void MoveToScreen(this System.Windows.Window window, Monitor next, bool fullScreen = false)
        {
            if (fullScreen)
            {
                User32.SetWindowPos(new System.Windows.Interop.WindowInteropHelper(window).Handle, (IntPtr)SpecialWindowHandles.Top,
                                    (int)next.NativeBounds.Left, (int)next.NativeBounds.Top, (int)next.NativeBounds.Width, (int)next.NativeBounds.Height, SetWindowPosFlags.ShowWindow);
                return;
            }

            User32.SetWindowPos(new System.Windows.Interop.WindowInteropHelper(window).Handle, (IntPtr)SpecialWindowHandles.Top,
                                (int)next.NativeBounds.Left, (int)next.NativeBounds.Top, (int)window.Width, (int)window.Height, SetWindowPosFlags.ShowWindow);
        }
Beispiel #2
0
        /// <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());
        }