/// <summary> /// Gets the screen rectangle that contains the point relative to the given visual /// </summary> public static Rect GetScreenRect(Point pt, Visual relativeTo) { // Validate if (relativeTo == null) { throw new ArgumentNullException("relativeTo"); } Point screenCoordinates = GetScreenCoordinates(pt, relativeTo); NativeMethods.POINT screenPoint = new NativeMethods.POINT(); screenPoint.x = (int)screenCoordinates.X; screenPoint.y = (int)screenCoordinates.Y; IntPtr monitor = NativeMethods.MonitorFromPoint(screenPoint, NativeMethods.MONITOR_DEFAULTTONEAREST); NativeMethods.MONITORINFO monitorInfo = new NativeMethods.MONITORINFO(); monitorInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(monitorInfo); if (NativeMethods.GetMonitorInfo(monitor, ref monitorInfo)) { return(new Rect(new Point(monitorInfo.rcWork.left, monitorInfo.rcWork.top), new Point(monitorInfo.rcWork.right, monitorInfo.rcWork.bottom))); } else { return(SystemParameters.WorkArea); } }
/// <summary> /// Gets the screen rectangle that contains given screen point. /// </summary> public static Rect GetScreenRect(Point screenCoordinates) { NativeMethods.POINT screenPoint = new NativeMethods.POINT(); screenPoint.x = (int)screenCoordinates.X; screenPoint.y = (int)screenCoordinates.Y; IntPtr monitor = NativeMethods.MonitorFromPoint(screenPoint, NativeMethods.MONITOR_DEFAULTTONEAREST); NativeMethods.MONITORINFO monitorInfo = new NativeMethods.MONITORINFO(); monitorInfo.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(monitorInfo); if (NativeMethods.GetMonitorInfo(monitor, ref monitorInfo)) { return(new Rect(new Point(monitorInfo.rcWork.left, monitorInfo.rcWork.top), new Point(monitorInfo.rcWork.right, monitorInfo.rcWork.bottom))); } else { return(SystemParameters.WorkArea); } }