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);
/// <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); }
public static bool FitInScreen(Window window) { var windowInteropHelper = new WindowInteropHelper(window); IntPtr hMonitor = MonitorFromWindow(windowInteropHelper.Handle, MONITOR_DEFAULTTONEAREST); if (hMonitor == IntPtr.Zero) { return(false); } var monitorInfo = MonitorInfoEx.Create(); if (GetMonitorInfo(hMonitor, ref monitorInfo) == false) { return(false); } DpiScale dpiInfo = VisualTreeHelper.GetDpi(App.Current.MainWindow); monitorInfo.Monitor.bottom = (int)Math.Floor(monitorInfo.Monitor.right / dpiInfo.DpiScaleX); monitorInfo.Monitor.bottom = (int)Math.Floor(monitorInfo.Monitor.bottom / dpiInfo.DpiScaleY); monitorInfo.WorkArea.right = (int)Math.Floor(monitorInfo.WorkArea.right / dpiInfo.DpiScaleX); monitorInfo.WorkArea.bottom = (int)Math.Floor(monitorInfo.WorkArea.bottom / dpiInfo.DpiScaleY); int top = (int)window.Top; if (top < monitorInfo.WorkArea.top) { window.Top = monitorInfo.WorkArea.top; } int bottom = (int)(window.Top + window.Height); if (bottom > monitorInfo.WorkArea.bottom) { if (window.Height < monitorInfo.WorkArea.bottom) { window.Top = monitorInfo.WorkArea.bottom - window.Height; } else { window.Top = monitorInfo.WorkArea.top; window.Height = monitorInfo.WorkArea.bottom - monitorInfo.WorkArea.top; } } int left = (int)window.Left; if (left < monitorInfo.WorkArea.left) { window.Left = monitorInfo.WorkArea.left; } int right = (int)(window.Left + window.Width); if (right > monitorInfo.WorkArea.right) { if (window.Width < monitorInfo.WorkArea.right) { window.Left = monitorInfo.WorkArea.right - window.Width; } else { window.Left = monitorInfo.WorkArea.left; window.Width = monitorInfo.WorkArea.right - monitorInfo.WorkArea.left; } } return(true); }