/// <summary> /// Create a new instance. /// </summary> private Screen(IntPtr monitorHandle) { var info = new MONITORINFOEX(); User32WindowFunctions.GetMonitorInfo(new HandleRef(null, monitorHandle), info); Bounds = new Rect(info.rcMonitor.left, info.rcMonitor.top, info.rcMonitor.width, info.rcMonitor.height); WorkingArea = new Rect(info.rcWork.left, info.rcWork.top, info.rcWork.width, info.rcWork.height); Primary = (info.dwFlags & MONITORINFOEX.FLAGS_PRIMARY) != 0; DeviceName = new string(info.szDevice).TrimEnd((char)0); var workingLocation = new Point(WorkingArea.Left - Bounds.Left, WorkingArea.Y - Bounds.Y); ScreenRelativeWorkingArea = new Rect(workingLocation, WorkingArea.Size); }
/// <summary> /// Get a list of all screens attached to the device. /// </summary> /// <returns></returns> public static Screen[] GetAllScreens() { var screens = new List<Screen>(); User32WindowFunctions.EnumDisplayMonitorsCallback callback = (monitor, hdc, lprcMonitor, lparam) => { screens.Add(new Screen(monitor)); return true; }; User32WindowFunctions.EnumDisplayMonitors(User32WindowFunctions.NullHandleRef, null, callback, IntPtr.Zero); return screens.ToArray(); }
/// <summary> /// Get the screen containing most of the given rectangle. /// </summary> public static Screen FromRect(Rect rect) { var nativeRect = new RECT((int)rect.Left, (int)rect.Top, (int)rect.Right, (int)rect.Bottom); return new Screen(User32WindowFunctions.MonitorFromRect(ref nativeRect, MONITOR_DEFAULTTO.NEAREST)); }
/// <summary> /// Get the screen containing the given point. /// </summary> public static Screen FromPoint(Point point) { var nativePoint = new POINTSTRUCT((int)point.X, (int)point.Y); return new Screen(User32WindowFunctions.MonitorFromPoint(nativePoint, MONITOR_DEFAULTTO.NEAREST)); }
/// <summary> /// Get the screen which has most of the window referred to by <paramref name="hwnd"/> on /// it. /// </summary> public static Screen FromHWnd(IntPtr hwnd) { return new Screen(User32WindowFunctions.MonitorFromWindow(new HandleRef(null, hwnd), MONITOR_DEFAULTTO.NEAREST)); }
/// <summary> /// Get the screen marked as primary (the one that the taskbar is on) /// </summary> public static Screen GetPrimaryScreen() { return new Screen(User32WindowFunctions.MonitorFromPoint(new POINTSTRUCT(), MONITOR_DEFAULTTO.PRIMARY)); }