Example #1
0
        public void TestGetBounds()
        {
            var displayInfo   = User32Api.AllDisplays().First();
            var displayBounds = DisplayInfo.GetBounds(displayInfo.Bounds.Location);

            Assert.Equal(displayInfo.Bounds, displayBounds);
        }
Example #2
0
 public void TestAllDisplays()
 {
     foreach (var display in User32Api.AllDisplays())
     {
         Log.Debug().WriteLine("Index {0} - Primary {3} - Device {1} - Bounds: {2}", display.Index, display.DeviceName, display.Bounds, display.IsPrimary);
     }
 }
Example #3
0
        /// <summary>
        ///     Check if a Windows Store App (WinRT) is visible
        /// </summary>
        /// <param name="windowBounds"></param>
        /// <returns>true if an app, covering the supplied rect, is visisble</returns>
        public static bool AppVisible(NativeRect windowBounds)
        {
            if (AppVisibility == null)
            {
                return(true);
            }

            foreach (var screen in User32Api.AllDisplays())
            {
                if (screen.Bounds.Contains(windowBounds))
                {
                    if (windowBounds.Equals(screen.Bounds))
                    {
                        // Fullscreen, it's "visible" when AppVisibilityOnMonitor says yes
                        // Although it might be the other App, this is not "very" important
                        var rect    = screen.Bounds;
                        var monitor = User32Api.MonitorFromRect(ref rect, MonitorFrom.DefaultToNearest);
                        if (monitor != IntPtr.Zero)
                        {
                            var monitorAppVisibility = AppVisibility.ComObject.GetAppVisibilityOnMonitor(monitor);
                            if (monitorAppVisibility == MonitorAppVisibility.MAV_APP_VISIBLE)
                            {
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        // Is only partly on the screen, when this happens the app is allways visible!
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #4
0
        /// <summary>
        ///     Get the bounds of the complete screen
        /// </summary>
        /// <returns></returns>
        public static NativeRect GetAllScreenBounds()
        {
            int left = 0, top = 0, bottom = 0, right = 0;

            foreach (var display in User32Api.AllDisplays())
            {
                left = Math.Min(left, display.Bounds.X);
                top  = Math.Min(top, display.Bounds.Y);
                var screenAbsRight  = display.Bounds.X + display.Bounds.Width;
                var screenAbsBottom = display.Bounds.Y + display.Bounds.Height;
                right  = Math.Max(right, screenAbsRight);
                bottom = Math.Max(bottom, screenAbsBottom);
            }
            return(new NativeRect(left, top, right + Math.Abs(left), bottom + Math.Abs(top)));
        }
Example #5
0
        /// <summary>
        ///     Implementation like <a href="https://msdn.microsoft.com/en-us/library/6d7ws9s4(v=vs.110).aspx">Screen.GetBounds</a>
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public static NativeRect GetBounds(NativePoint point)
        {
            DisplayInfo returnValue = null;

            foreach (var display in User32Api.AllDisplays())
            {
                if (display.IsPrimary && returnValue == null)
                {
                    returnValue = display;
                }
                if (display.Bounds.Contains(point))
                {
                    returnValue = display;
                }
            }
            return(returnValue?.Bounds ?? Rect.Empty);
        }
        /// <summary>
        ///     Get a location where this window would be visible
        ///     * if none is found return false, formLocation = the original location
        ///     * if something is found, return true and formLocation = new location
        /// </summary>
        /// <param name="interopWindow">IInteropWindow, the window to find a location for</param>
        /// <param name="formLocation">NativePoint with the location where the window will fit</param>
        /// <returns>true if a location if found, and the formLocation is also set</returns>
        public static bool GetVisibleLocation(this IInteropWindow interopWindow, out NativePoint formLocation)
        {
            bool doesWindowFit   = false;
            var  windowRectangle = interopWindow.GetInfo().Bounds;

            // assume own location
            formLocation = windowRectangle.Location;
            var primaryDisplay = User32Api.AllDisplays().First(x => x.IsPrimary);

            using (var workingArea = new Region(primaryDisplay.Bounds))
            {
                // Create a region with the screens working area
                foreach (var display in User32Api.AllDisplays())
                {
                    if (!display.IsPrimary)
                    {
                        workingArea.Union(display.Bounds);
                    }
                }

                // If the formLocation is not inside the visible area
                if (!workingArea.AreRectangleCornersVisisble(windowRectangle))
                {
                    // If none found we find the biggest screen
                    foreach (var display in User32Api.AllDisplays())
                    {
                        var newWindowRectangle = new Rectangle(display.WorkingArea.Location, windowRectangle.Size);
                        if (!workingArea.AreRectangleCornersVisisble(newWindowRectangle))
                        {
                            continue;
                        }
                        formLocation  = display.Bounds.Location;
                        doesWindowFit = true;
                        break;
                    }
                }
                else
                {
                    doesWindowFit = true;
                }
            }
            return(doesWindowFit);
        }