/// <summary> /// /// </summary> /// <param name="window"></param> /// <returns></returns> public static Point GetMonitorSize(Window window) { // Get handle for nearest monitor to this window WindowInteropHelper wih = new WindowInteropHelper(window); IntPtr hMonitor = WinApi.MonitorFromWindow(wih.Handle, WinApi.MONITOR_DEFAULTTONEAREST); // Get monitor info WinApi.MonitorInfoEx monitorInfo = new WinApi.MonitorInfoEx(); monitorInfo.cbSize = Marshal.SizeOf(monitorInfo); WinApi.GetMonitorInfo(new HandleRef(window, hMonitor), monitorInfo); // Create working area dimensions, converted to DPI-independent values HwndSource source = HwndSource.FromHwnd(wih.Handle); if (source == null) return new Point(); // Should never be null if (source.CompositionTarget == null) return new Point(); // Should never be null Matrix matrix = source.CompositionTarget.TransformFromDevice; WinApi.Rect workingArea = monitorInfo.rcWork; Point dpiIndependentSize = matrix.Transform(new Point(workingArea.Right - workingArea.Left, workingArea.Bottom - workingArea.Top)); return dpiIndependentSize; }
/// <summary> /// /// </summary> /// <param name="window"></param> /// <returns></returns> public static Point GetMonitorSize(Window window) { // Get handle for nearest monitor to this window WindowInteropHelper wih = new WindowInteropHelper(window); IntPtr hMonitor = WinApi.MonitorFromWindow(wih.Handle, WinApi.MONITOR_DEFAULTTONEAREST); // Get monitor info WinApi.MonitorInfoEx monitorInfo = new WinApi.MonitorInfoEx(); monitorInfo.cbSize = Marshal.SizeOf(monitorInfo); WinApi.GetMonitorInfo(new HandleRef(window, hMonitor), monitorInfo); // Create working area dimensions, converted to DPI-independent values HwndSource source = HwndSource.FromHwnd(wih.Handle); if (source == null) { return(new Point()); // Should never be null } if (source.CompositionTarget == null) { return(new Point()); // Should never be null } Matrix matrix = source.CompositionTarget.TransformFromDevice; WinApi.Rect workingArea = monitorInfo.rcWork; Point dpiIndependentSize = matrix.Transform(new Point(workingArea.Right - workingArea.Left, workingArea.Bottom - workingArea.Top)); return(dpiIndependentSize); }