/// <summary> /// Gets the DPI of the monitor nearest to a window /// </summary> /// <param name="hWnd">Handle to the window</param> /// <returns>DPI of the monitor nearest to the window</returns> private static DpiScale2 GetDpiForWindowFromNearestMonitor(IntPtr hWnd) { IntPtr hMon = SafeNativeMethods.MonitorFromWindow(new HandleRef(IntPtr.Zero, hWnd), NativeMethods.MONITOR_DEFAULTTONEAREST); uint dpiX, dpiY; int hr = (int)UnsafeNativeMethods.GetDpiForMonitor(new HandleRef(IntPtr.Zero, hMon), MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out dpiX, out dpiY); // Throw if FAILED(hr) Marshal.ThrowExceptionForHR(hr); return(DpiScale2.FromPixelsPerInch(dpiX, dpiY)); }
private static NativeMethods.MONITORINFOEX NearestMonitorInfoFromWindow(IntPtr hwnd) { IntPtr hMonitor = SafeNativeMethods.MonitorFromWindow(new HandleRef(null, hwnd), NativeMethods.MONITOR_DEFAULTTONEAREST); if (hMonitor == IntPtr.Zero) { throw new Win32Exception(); } var monitorInfo = new NativeMethods.MONITORINFOEX(); SafeNativeMethods.GetMonitorInfo(new HandleRef(null, hMonitor), monitorInfo); return(monitorInfo); }