/// <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); }
/// <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 AllDisplayInfos) { if (display.IsPrimary && returnValue == null) { returnValue = display; } if (display.Bounds.Contains(point)) { returnValue = display; } } return(returnValue?.Bounds ?? NativeRect.Empty); }