Example #1
0
 internal MonitorInfo(MonitorInfoEx mex)
 {
     this.ViewportBounds = mex.rcMonitor;
     this.WorkAreaBounds = (Rect)mex.rcWork;
     this.IsPrimary      = mex.dwFlags.HasFlag(MonitorInfoF.Primary);
     this.DeviceId       = mex.szDevice;
 }
Example #2
0
        private static Rect GetWorkAreaRect()
        {
            MonitorInfoEx monitorInfoEx = new MonitorInfoEx();

            bool MonitorEnumCallBack(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
            {
                monitorInfoEx.Size = Marshal.SizeOf(monitorInfoEx);
                GetMonitorInfo(hMonitor, ref monitorInfoEx);
                return(true);
            }

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, MonitorEnumCallBack, IntPtr.Zero);

            Rect rect = monitorInfoEx.WorkArea;

            if (rect.Right > 3000)
            {
                // For ultra wide monitors work area should be only on the left side of the screen,
                //   where right side is reserved for the Visual Studio instance.
                //
                rect.Right /= 2;
            }

            return(rect);
        }
Example #3
0
        /// <summary>
        /// Returns the number of Displays using the Win32 functions
        /// </summary>
        /// <returns>collection of Display Info</returns>
        public DisplayInfoCollection GetDisplays()
        {
            DisplayInfoCollection col = new DisplayInfoCollection();

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                                delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData)
            {
                MonitorInfoEx mi = new MonitorInfoEx();
                mi.Size          = Marshal.SizeOf(mi);
                bool success     = GetMonitorInfo(hMonitor, ref mi);
                if (success)
                {
                    DisplayInfo di  = new DisplayInfo();
                    di.hdcMonitor   = hdcMonitor;
                    di.hMonitor     = hMonitor;
                    di.ScreenWidth  = (mi.Monitor.Right - mi.Monitor.Left);
                    di.ScreenHeight = (mi.Monitor.Bottom - mi.Monitor.Top);
                    di.MonitorArea  = mi.Monitor;
                    di.WorkArea     = mi.WorkArea;
                    di.Availability = mi.Flags.ToString();
                    di.DeviceName   = mi.DeviceName;

                    col.Add(di);
                }
                return(true);
            }, IntPtr.Zero);
            return(col);
        }
Example #4
0
        private static List <DisplayInfo> GetDisplays()
        {
            List <DisplayInfo>  displayCollection = new List <DisplayInfo>();
            MonitorEnumDelegate monitorDelegate   = delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
            {
                MonitorInfoEx monitorInfo = new MonitorInfoEx();
                monitorInfo.size = (uint)Marshal.SizeOf(monitorInfo);
                if (GetMonitorInfo(hMonitor, ref monitorInfo))
                {
                    var info = new DevMode();
                    EnumDisplaySettings(monitorInfo.deviceName, -1, ref info);

                    var monitor = new Rect
                    {
                        left   = info.dmPositionX,
                        right  = info.dmPositionX + info.dmPelsWidth,
                        top    = info.dmPositionY,
                        bottom = info.dmPositionY + info.dmPelsHeight
                    };

                    DisplayInfo displayInfo = new DisplayInfo(monitor, monitorInfo.flags);
                    displayCollection.Add(displayInfo);
                }
                return(true);
            };

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, monitorDelegate, IntPtr.Zero);
            return(displayCollection);
        }
        public static IEnumerable <MonitorInfo> GetMonitors()
        {
            var result = new List <MonitorInfo>();

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                                delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
            {
                MonitorInfoEx mi = new MonitorInfoEx();
                mi.Size          = Marshal.SizeOf(mi);
                bool success     = GetMonitorInfo(hMonitor, ref mi);
                if (success)
                {
                    var info = new MonitorInfo
                    {
                        ScreenSize  = new Vector2(mi.Monitor.right - mi.Monitor.left, mi.Monitor.bottom - mi.Monitor.top),
                        MonitorArea = new Rect(mi.Monitor.left, mi.Monitor.top, mi.Monitor.right - mi.Monitor.left, mi.Monitor.bottom - mi.Monitor.top),
                        WorkArea    = new Rect(mi.WorkArea.left, mi.WorkArea.top, mi.WorkArea.right - mi.WorkArea.left, mi.WorkArea.bottom - mi.WorkArea.top),
                        IsPrimary   = mi.Flags > 0,
                        Hmon        = hMonitor,
                        DeviceName  = mi.DeviceName
                    };
                    result.Add(info);
                }
                return(true);
            }, IntPtr.Zero);
            return(result);
        }
Example #6
0
        static public List <Screen> GetAllScreens()
        {
            List <Screen> screens = new List <Screen>();

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                                delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
            {
                MonitorInfoEx mi = new MonitorInfoEx();
                mi.Size          = (int)Marshal.SizeOf(mi);
                bool success     = GetMonitorInfo(hMonitor, ref mi);
                if (success)
                {
                    Screen screen = new Screen()
                    {
                        ScreenArea = new Rect(mi.Monitor.Left, mi.Monitor.Top, mi.Monitor.Right - mi.Monitor.Left, mi.Monitor.Bottom - mi.Monitor.Top),
                        WorkArea   = new Rect(mi.WorkArea.Left, mi.WorkArea.Top, mi.WorkArea.Right - mi.WorkArea.Left, mi.WorkArea.Bottom - mi.WorkArea.Top),
                        IsPrimary  = (mi.Flags & 1) == 1,
                        Name       = mi.DeviceName
                    };
                    screens.Add(screen);
                }
                return(true);
            }, IntPtr.Zero);

            return(screens);
        }
Example #7
0
        internal static List <ScreenInfo> GetScreensInfos()
        {
            var scrInfos = new List <ScreenInfo>();

            EnumDisplayMonitors(
                IntPtr.Zero,
                IntPtr.Zero,
                delegate(
                    IntPtr hMonitor,
                    IntPtr hdcMonitor,
                    ref Rect lprcMonitor,
                    IntPtr dwData)
            {
                var mi       = new MonitorInfoEx();
                mi.Size      = (int)Marshal.SizeOf(mi);
                bool success = GetMonitorInfo(hMonitor, ref mi);
                if (success)
                {
                    var di = new ScreenInfo
                    {
                        Width      = (mi.Monitor.Right - mi.Monitor.Left),
                        Height     = (mi.Monitor.Bottom - mi.Monitor.Top),
                        IsPrimary  = mi.Flags == MONITORINFOF_PRIMARY,
                        DeviceName = mi.DeviceName
                    };
                    di.MonitorArea = Area.FromRect(di, mi.Monitor);
                    di.WorkArea    = Area.FromRect(di, mi.WorkArea);
                    scrInfos.Add(di);
                }
                return(true);
            }, IntPtr.Zero);
            return(scrInfos);
        }
        private bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
        {
            var mi = new MonitorInfoEx();

            mi.cbSize = Marshal.SizeOf(mi);

            GetMonitorInfoEx(MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY), ref mi);

            Process process = _retryPolicy.Execute(() =>
            {
                return(Process.GetProcessesByName(LeagueClientProcessName)?.FirstOrDefault());
            }, proc => proc != null && proc.MainWindowHandle != IntPtr.Zero, maxRetries: 10, delayBetweenRequest: 1000);

            if (process == null || process.MainWindowHandle == IntPtr.Zero)
            {
                throw new LeagueOfLegendsProcessException($"No League clien process found");
            }
            WINDOWPLACEMENT placement = GetPlacement(process.MainWindowHandle);

            int monitorHeight = mi.rcMonitor.Right - mi.rcMonitor.Left;
            int monitorWidth  = mi.rcMonitor.Bottom - mi.rcMonitor.Top;
            int windowHeight  = placement.rcNormalPosition.Right - placement.rcNormalPosition.Left;
            int windowWidth   = placement.rcNormalPosition.Bottom - placement.rcNormalPosition.Top;

            return(windowHeight == monitorHeight && windowWidth == monitorWidth);
        }
Example #9
0
        /// <summary>
        /// Returns the number of Displays using the Win32 functions
        /// </summary>
        /// <returns>collection of Display Info</returns>
        public static IReadOnlyCollection <MonitorInfoEx> EnumDisplayMonitors()
        {
            var result = new List <MonitorInfoEx>();

            var enumSuccess = EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                                                  delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
            {
                var mi  = new MonitorInfoEx();
                mi.Size = Marshal.SizeOf(mi);

                bool success = GetMonitorInfo(hMonitor, ref mi);
                if (!success)
                {
                    Console.Error.WriteLine(new Win32Exception());
                }
                else
                {
                    result.Add(mi);
                }
                // say we want to continue enumerating
                return(true);
            }, IntPtr.Zero);

            if (!enumSuccess)
            {
                throw new Win32Exception();
            }

            return(result.AsReadOnly());
        }
Example #10
0
        /// <summary>
        ///     Returns the number of Displays using the Win32 functions
        /// </summary>
        /// <returns>collection of Display Info</returns>
        public static IEnumerable <DisplayInfo> AllDisplays()
        {
            var result = new List <DisplayInfo>();
            int index  = 1;

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr monitor, IntPtr hdcMonitor, ref NativeRect lprcMonitor, IntPtr data) =>
            {
                var monitorInfoEx = MonitorInfoEx.Create();
                var success       = GetMonitorInfo(monitor, ref monitorInfoEx);
                if (!success)
                {
                    return(true);
                }
                var displayInfo = new DisplayInfo
                {
                    Index        = index++,
                    ScreenWidth  = Math.Abs(monitorInfoEx.Monitor.Right - monitorInfoEx.Monitor.Left),
                    ScreenHeight = Math.Abs(monitorInfoEx.Monitor.Bottom - monitorInfoEx.Monitor.Top),
                    Bounds       = monitorInfoEx.Monitor,
                    WorkingArea  = monitorInfoEx.WorkArea,
                    IsPrimary    = (monitorInfoEx.Flags & MonitorInfoFlags.Primary) == MonitorInfoFlags.Primary
                };
                result.Add(displayInfo);
                return(true);
            }, IntPtr.Zero);
            return(result);
        }
Example #11
0
        public static IList <Display> GetDisplays()
        {
            var list  = new List <Display>();
            int index = 1;

            NativeMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr monitor, IntPtr _, ref NativeRect __, IntPtr ___) =>
            {
                var monitorInfoEx = MonitorInfoEx.Create();
                if (!NativeMethods.GetMonitorInfo(monitor, ref monitorInfoEx))
                {
                    return(true);
                }

                NativeDpiMethods.GetDpiForMonitor(monitor, MonitorDpiType.EffectiveDpi, out var dpiX, out var dpiY);

                var display = new Display
                {
                    Handle      = monitor,
                    Index       = index++,
                    Bounds      = (Rectangle)monitorInfoEx.Monitor,
                    WorkingArea = (Rectangle)monitorInfoEx.WorkArea,
                    IsPrimary   = (monitorInfoEx.Flags & MonitorInfoFlags.Primary) == MonitorInfoFlags.Primary,
                    DeviceName  = monitorInfoEx.DeviceName,
                    Dpi         = new Point((int)dpiX, (int)dpiY),
                };
                list.Add(display);

                return(true);
            }, IntPtr.Zero);
Example #12
0
        private void MainWindow_LocationChanged(object sender, EventArgs e)
        {
            IntPtr windowHandle  = new WindowInteropHelper(Application.Current.MainWindow).Handle;
            var    monitorHandle = MonitorFromWindow(windowHandle, MonitorDefaultToNearest);

            // Get the logical width and height of the monitor.
            var monitorInfo = new MonitorInfoEx();

            monitorInfo.Size = Marshal.SizeOf(monitorInfo);

            GetMonitorInfoEx(monitorHandle, ref monitorInfo);
            int cxLogical = (monitorInfo.MonitorArea.Right - monitorInfo.MonitorArea.Left);

            // Get the physical width and height of the monitor.
            DevMode dm = new DevMode();

            dm.dmSize        = (short)Marshal.SizeOf(dm);
            dm.dmDriverExtra = 0;
            EnumDisplaySettings(monitorInfo.DeviceName, EnumCurrentSettings, ref dm);
            int cxPhysical = dm.dmPelsWidth;

            // Calculate the scaling factor.
            double scaleFactor = ((double)cxPhysical / (double)cxLogical);

            Scale.Text = scaleFactor.ToString();
        }
Example #13
0
        private static bool EnumMonitorsProc(IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData)
        {
            MonitorInfoEx mi = new MonitorInfoEx();

            mi.Size = (uint)Marshal.SizeOf(mi);

            bool success = GetMonitorInfo(hMonitor, ref mi);

            if (success)
            {
                ScreenInfo si = new ScreenInfo();
                si.MonitorArea     = Oblong.FromRectStruct(mi.Monitor);
                si.WorkArea        = Oblong.FromRectStruct(mi.WorkArea);
                si.DeviceName      = mi.DeviceName;
                si.IsPrimaryScreen = ((mi.Flags & MONITORINFOF_PRIMARY) == 1);;

                DEVMODE DeviceMode = new DEVMODE();
                DeviceMode.Initialize();

                if (EnumDisplaySettingsEx(ToLPTStr(mi.DeviceName), -1, ref DeviceMode))
                {
                    si.Scaling = Math.Round(((double)DeviceMode.dmPelsHeight / (mi.Monitor.bottom - mi.Monitor.top)) * 100);
                }

                si.NativeWorkArea = new Oblong((int)(mi.WorkArea.left * si.Scaling) / 100, (int)(mi.WorkArea.top * si.Scaling) / 100, (int)(mi.WorkArea.right * si.Scaling) / 100, (int)(mi.WorkArea.bottom * si.Scaling) / 100);
                si.NativeArea     = new Oblong((int)(mi.Monitor.left * si.Scaling) / 100, (int)(mi.Monitor.top * si.Scaling) / 100, (int)(mi.Monitor.right * si.Scaling) / 100, (int)(mi.Monitor.bottom * si.Scaling) / 100);

                Display.Screens.Add(si);
            }

            return(true);
        }
    public Form1()
    {
        InitializeComponent();
        String        thisFormsMonitor = null;
        IntPtr        hMon             = MonitorFromWindow(this.Handle, 0);
        MonitorInfoEx monInfo          = new MonitorInfoEx();

        monInfo.Size = 104;
        if (GetMonitorInfo(hMon, ref monInfo))
        {
            thisFormsMonitor = monInfo.DeviceName;
        }
        DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();

        displayDevice.cb = Marshal.SizeOf(displayDevice);
        uint deviceIndex = 0;

        while (EnumDisplayDevices(null, deviceIndex, ref displayDevice, 0))
        {
            if (displayDevice.DeviceName == thisFormsMonitor)
            {
                System.Diagnostics.Debug.WriteLine(displayDevice.DeviceID);
            }
            deviceIndex++;
        }
        this.Text = System.Windows.Forms.Screen.PrimaryScreen.DeviceName;
    }
Example #15
0
        private static void PrintCurrentMonitor()
        {
            var foregroundWindow = GetForegroundWindow();
            var monitorInfo      = new MonitorInfoEx().Init();

            GetMonitorInfo(MonitorFromWindow(foregroundWindow, MonitorDefault.Primary), ref monitorInfo);
            Console.WriteLine(monitorInfo.DeviceName + " " + monitorInfo.WorkArea);
        }
Example #16
0
            public string     DeviceName;     //The device name of the monitor

            public static MonitorInfoEx CreateWithDefaults()
            {
                var mi = new MonitorInfoEx();

                mi.DeviceName = String.Empty;
                mi.Size       = Marshal.SizeOf(mi);
                return(mi);
            }
        public static List <DisplayInfo> GetDisplays()
        {
            var list = new List <DisplayInfo>();

            try {
                User32.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                                           (IntPtr hMonitor, IntPtr hdcMonitor, ref NativeRect lprcMonitor, IntPtr dwData) => {
                    var mi = new MonitorInfoEx();
                    mi.Init();
                    mi.size     = Marshal.SizeOf(mi);
                    mi.size     = 72;
                    var success = User32.GetMonitorInfo(hMonitor, ref mi);
                    if (success)
                    {
                        var di            = new DisplayInfo();
                        di.MonitorArea    = mi.monitor;
                        di.WorkArea       = mi.work;
                        di.PrimaryDisplay = (mi.flags & 1) != 0;

                        di.LogicalScreenHeight  = GDI32.GetDeviceCaps(hMonitor, (int)GDI32.DeviceCap.VERTRES);
                        di.PhysicalScreenHeight = GDI32.GetDeviceCaps(hMonitor, (int)GDI32.DeviceCap.DESKTOPVERTRES);

                        // TransformToPixels(0, 0, out var x, out var y);

                        uint dpiX;
                        uint dpiY;

                        try {
                            ShCore.GetDpiForMonitor(
                                hMonitor,
                                MonitorDpiType.MDT_EFFECTIVE_DPI,
                                out dpiX,
                                out dpiY
                                );
                        } catch {
                            dpiX = 96;
                            dpiY = 96;
                        }

                        di.scaleFactor2 = dpiX / 96f;
                        list.Add(di);
                    }
                    else
                    {
                        Logger.Debug("Getting monitor info failed");
                    }

                    return(true);
                }, IntPtr.Zero);

                AddAdditionalInfos(list);
            } catch (Exception e) {
                Logger.Exception(e);
            }

            return(list);
        }
Example #18
0
 private static Action SetWindowPosAction(Func <Rect, Rect> workAreaToWindowPos)
 {
     return(() => {
         var foregroundWindow = GetForegroundWindow();
         var monitorInfo = new MonitorInfoEx().Init();
         GetMonitorInfo(MonitorFromWindow(foregroundWindow, MonitorDefault.Primary), ref monitorInfo);
         SetWindowPos(foregroundWindow, workAreaToWindowPos(monitorInfo.WorkArea));
     });
 }
Example #19
0
        private static bool EnumDisplayMonitorsCallback(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
        {
            MonitorInfoEx monitorInfo = new MonitorInfoEx().Init();

            GetMonitorInfo(hMonitor, ref monitorInfo);
            mMonitors.Add(monitorInfo);
            Console.WriteLine($"{monitorInfo.DeviceName}: {monitorInfo.WorkArea}");
            return(true);
        }
Example #20
0
        private static bool MonitorEnumCallBack(IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData)
        {
            MonitorInfoEx mon_info = new MonitorInfoEx();

            mon_info.Init();
            mon_info.Size = Marshal.SizeOf(mon_info);
            GetMonitorInfo(hMonitor, ref mon_info);
            ///Monitor info is stored in 'mon_info'
            return(true);
        }
Example #21
0
        private ScreenHelper(IntPtr monitor, IntPtr hdc)
        {
            var info = new MonitorInfoEx();
            GetMonitorInfo(new HandleRef(null, monitor), info);

            Bounds = info.rcMonitor.Rect;
            WorkingArea = info.rcWork.Rect;
            IsPrimary = ((info.dwFlags & MonitorinfofPrimary) != 0);
            Name = new string(info.szDevice).TrimEnd((char) 0);
        }
        internal bool IsPrimary()
        {
            MonitorInfoEx monitorInfo = new MonitorInfoEx();

            monitorInfo.Size = Marshal.SizeOf(monitorInfo);

            GetMonitorInfo(_description.MonitorHandle, ref monitorInfo);

            return((monitorInfo.Flags & MONITORINFOF_PRIMARY) == MONITORINFOF_PRIMARY);
        }
        public void UpdateMonitorInfo()
        {
            MonitorInfoEx mi = new MonitorInfoEx();

            mi.Size = Marshal.SizeOf(mi);
            if (GetMonitorInfo(Handle, ref mi))
            {
                Info = mi;
            }
        }
Example #24
0
        public static MonitorInfoEx GetScreenFromWindow(IntPtr hwnd)
        {
            IntPtr        hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
            MonitorInfoEx mi       = new MonitorInfoEx();

            mi.Size = (uint)Marshal.SizeOf(mi);

            bool success = GetMonitorInfo(hMonitor, ref mi);

            return(mi);
        }
Example #25
0
        internal static Size ScreenSizeFromWindow(IntPtr handle)
        {
            var pointer = MonitorFromWindow(handle, MonitorDefaultToNearest);

            var info = new MonitorInfoEx();

            GetMonitorInfo(pointer, ref info);

            var rect = info.rcWork.ToRectangle();

            return(new Size(rect.Width, rect.Height));
        }
Example #26
0
        private static MonitorInfoEx GetMonitorInfo(IntPtr hMonitor)
        {
            var info = new MonitorInfoEx()
            {
                cbSize = Marshal.SizeOf(typeof(MonitorInfoEx)),
            };

            if (!GetMonitorInfo(hMonitor, ref info))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            return(info);
        }
 public Win32.CMonitor.MONITORINFOEX Copy2Win32Struct(MonitorInfoEx monitorInfoEx)
 {
     var buf = new Win32.CMonitor.MONITORINFOEX();
     foreach (PropertyInfo propInfo in monitorInfoEx.GetType().GetProperties())
     {
         foreach (PropertyInfo propInfoInBuf in buf.GetType().GetProperties())
         {
             if (propInfo.Name == propInfoInBuf.Name)
                 propInfoInBuf.SetValue(buf, propInfo.GetValue(monitorInfoEx, null), null);
         }
     }
     return buf;
 }
Example #28
0
        /// <summary>
        ///     Enumerates all monitors connected to the system.
        /// </summary>
        /// <returns>A list of objects describing all monitors connected to the system.</returns>
        public static IEnumerable <MonitorInfo> GetMonitors()
        {
            var result = new List <MonitorInfo>();

            bool CreateMonitorInfoItem(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData)
            {
                var monitor = new MonitorInfoEx();

                monitor.Size = Marshal.SizeOf(monitor);

                if (GetMonitorInfo(hMonitor, ref monitor))
                {
                    result.Add(new MonitorInfo
                    {
                        ScreenSize = new Vector2(monitor.Monitor.right - monitor.Monitor.left,
                                                 monitor.Monitor.bottom - monitor.Monitor.top),
                        MonitorArea = new Rect(monitor.Monitor.left, monitor.Monitor.top,
                                               monitor.Monitor.right - monitor.Monitor.left,
                                               monitor.Monitor.bottom - monitor.Monitor.top),
                        WorkArea = new Rect(monitor.WorkArea.left, monitor.WorkArea.top,
                                            monitor.WorkArea.right - monitor.WorkArea.left,
                                            monitor.WorkArea.bottom - monitor.WorkArea.top),
                        IsPrimary  = monitor.Flags > 0,
                        Hmon       = hMonitor,
                        DeviceName = monitor.DeviceName
                    });
                }

                return(true);
            }

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, CreateMonitorInfoItem, IntPtr.Zero);

            // Sorting to make primary monitors be in front
            result.Sort((x, y) =>
            {
                if (x != null && x.IsPrimary)
                {
                    return(-1);
                }

                if (y != null && y.IsPrimary)
                {
                    return(1);
                }

                return(0);
            });

            return(result);
        }
Example #29
0
        internal static Size ScreenSizeFromPoint(int left, int top)
        {
            var pointer = MonitorFromPoint(new PointW {
                X = left, Y = top
            }, MonitorDefaultToNearest);

            var info = new MonitorInfoEx();

            GetMonitorInfo(new HandleRef(null, pointer), info);

            var rect = info.rcWork.ToRectangle();

            return(new Size(rect.Width, rect.Height));
        }
Example #30
0
        public static WindowInfo GetWindowInfo(IntPtr hwnd)
        {
            RectStruct WindowRect          = new RectStruct();
            RectStruct ExtendedFrameBounds = new RectStruct();
            RectStruct Border = new RectStruct();
            WindowInfo result = new WindowInfo();
            WindowSize Size   = new WindowSize();
            WindowSize SizeExtendedFrameBounds = new WindowSize();

            int size = Marshal.SizeOf(typeof(RectStruct));

            if (GetWindowRect(hwnd, out WindowRect))
            {
                DwmGetWindowAttribute(hwnd, DWMWINDOWATTRIBUTE.ExtendedFrameBounds, out ExtendedFrameBounds, size);
            }

            IntPtr        screen        = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
            MonitorInfoEx currentScreen = GetScreenFromWindow(hwnd);

            result.ScreenName    = currentScreen.DeviceName;
            result.ScalingFactor = getMonitorScaling(currentScreen);
            result.hwnd          = hwnd;

            // Update the rectangle with the current monitor scaling
            WindowRect.left   = (int)(WindowRect.left * result.ScalingFactor);
            WindowRect.top    = (int)(WindowRect.top * result.ScalingFactor);
            WindowRect.right  = (int)(WindowRect.right * result.ScalingFactor);
            WindowRect.bottom = (int)(WindowRect.bottom * result.ScalingFactor);

            // https://stackoverflow.com/questions/34139450/getwindowrect-returns-a-size-including-invisible-borders
            Border.left   = ExtendedFrameBounds.left - WindowRect.left;
            Border.top    = ExtendedFrameBounds.top - WindowRect.top;
            Border.right  = WindowRect.right - ExtendedFrameBounds.right;
            Border.bottom = WindowRect.bottom - ExtendedFrameBounds.bottom;

            result.WindowRect          = WindowRect;
            result.ExtendedFrameBounds = ExtendedFrameBounds;
            result.Border = Border;

            Size.height = WindowRect.bottom - WindowRect.top;
            Size.width  = WindowRect.right - WindowRect.left;

            SizeExtendedFrameBounds.height = ExtendedFrameBounds.bottom - ExtendedFrameBounds.top;
            SizeExtendedFrameBounds.width  = ExtendedFrameBounds.right - ExtendedFrameBounds.left;

            result.SizeWindow = Size;
            result.SizeExtendedFrameBounds = SizeExtendedFrameBounds;

            return(result);
        }
Example #31
0
        public static Size ScreenSizeFromWindow(IntPtr handle)
        {
            var pointer = User32.MonitorFromWindow(handle, Constants.MonitorDefaultToNearest);

            var info = new MonitorInfoEx();

            User32.GetMonitorInfo(new HandleRef(null, pointer), info);

            var rect = info.rcWork.ToRectangle();

            Gdi32.DeleteObject(pointer);

            return(new Size(rect.Width, rect.Height));
        }
Example #32
0
        /// <summary>
        /// Returns informations about the connect Mointors
        /// </summary>
        /// <returns>collection of Display Info</returns>
        public List <DisplayInfo> GetDisplays()
        {
            var col = new List <DisplayInfo>();

            bool Result(IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData)
            {
                var mi = new MonitorInfoEx();

                mi.Size = Marshal.SizeOf(mi);

                var success = GetMonitorInfo(hMonitor, ref mi);

                if (!success)
                {
                    //TODO Error Handling
                    var err = Marshal.GetLastWin32Error();
                    return(false);
                }

                var dev = new DISPLAY_DEVICE();

                dev.cb = Marshal.SizeOf(dev);
                if (!EnumDisplayDevices(mi.DeviceName, 0, ref dev, 1))
                {
                    //TODO Error Handling
                    var err = Marshal.GetLastWin32Error();
                }

                var di = new DisplayInfo
                {
                    ScreenWidth      = mi.Monitor.Right - mi.Monitor.Left,
                    ScreenHeight     = mi.Monitor.Bottom - mi.Monitor.Top,
                    MonitorArea      = mi.Monitor,
                    WorkArea         = mi.WorkArea,
                    IsPrimaryMonitor = Convert.ToBoolean(mi.Flags),
                    DeviceName       = dev.DeviceString,
                    DeviceId         = dev.DeviceID,
                    Handle           = hMonitor
                };

                Debug.Print($"Display Found: {di.DeviceName}");
                col.Add(di);
                return(true);
            }

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, Result, IntPtr.Zero);

            return(col);
        }
Example #33
0
 private Monitor(IntPtr monitor)
 {
     var info = new MonitorInfoEx();
     GetMonitorInfo(new HandleRef(null, monitor), info);
     Bounds = new System.Windows.Rect(
                 info.rcMonitor.left, info.rcMonitor.top,
                 info.rcMonitor.right - info.rcMonitor.left,
                 info.rcMonitor.bottom - info.rcMonitor.top);
     WorkingArea = new System.Windows.Rect(
                 info.rcWork.left, info.rcWork.top,
                 info.rcWork.right - info.rcWork.left,
                 info.rcWork.bottom - info.rcWork.top);
     IsPrimary = ((info.dwFlags & MonitorinfofPrimary) != 0);
     Name = new string(info.szDevice).TrimEnd((char)0);
 }
Example #34
0
 static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfoEx lpmi);
Example #35
0
        public static List<Screen> GetAllScreens()
        {
            List<Screen> screens = new List<Screen>();

            EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
                    delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData)
                    {
                        MonitorInfoEx mi = new MonitorInfoEx();
                        mi.Size = (int)Marshal.SizeOf(mi);
                        bool success = GetMonitorInfo(hMonitor, ref mi);
                        if (success)
                        {
                            Screen screen = new Screen()
                            {
                                ScreenArea = new Rect(mi.Monitor.Left, mi.Monitor.Top, mi.Monitor.Right - mi.Monitor.Left, mi.Monitor.Bottom - mi.Monitor.Top),
                                WorkArea = new Rect(mi.WorkArea.Left, mi.WorkArea.Top, mi.WorkArea.Right - mi.WorkArea.Left, mi.WorkArea.Bottom - mi.WorkArea.Top),
                                IsPrimary = (mi.Flags & 1) == 1,
                                Name = mi.DeviceName
                            };
                            screens.Add(screen);
                        }
                        return true;
                    }, IntPtr.Zero);

            return screens;
        }
		private string displayConfigFriendlyName;	// provided by DisplayConfig (if supported)



		/// <summary>Initializes a new <see cref="DisplayMonitor"/> instance.</summary>
		/// <param name="displayDevice">A valid <see cref="DisplayDevice"/> structure.</param>
		/// <param name="monitorHandle">A handle (HMONITOR) to the monitor.</param>
		internal DisplayMonitor( DisplayDevice displayDevice, IntPtr monitorHandle )
			: base( displayDevice )
		{
			info = GetMonitorInfo( handle = monitorHandle );
		}