From 234a4ea1ceb3c78168a41732e14f6e525c69b6b1 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Mon, 20 Apr 2015 10:27:40 -0700 Subject: [PATCH] Allow querying scale ratio for arbitrary hwnds --- PerMonitorDpiBehavior.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/PerMonitorDpiBehavior.cs b/PerMonitorDpiBehavior.cs index 2f59d4d..50691c4 100644 --- a/PerMonitorDpiBehavior.cs +++ b/PerMonitorDpiBehavior.cs @@ -23,18 +23,18 @@ public static bool IsHighDpiMethodSupported() return isHighDpiMethodSupported.Value; } - public static double GetScaleRatioForWindow(Window This) + public static double GetScaleRatioForWindow(IntPtr hWnd) { var wpfDpi = 96.0 * PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice.M11; - var hwndSource = PresentationSource.FromVisual(This) as HwndSource; if (IsHighDpiMethodSupported() == false) { + // Use System DPI return wpfDpi / 96.0; } else { - var monitor = SafeNativeMethods.MonitorFromWindow(hwndSource.Handle, MonitorOpts.MONITOR_DEFAULTTONEAREST); + var monitor = SafeNativeMethods.MonitorFromWindow(hWnd, MonitorOpts.MONITOR_DEFAULTTONEAREST); uint dpiX; uint dpiY; SafeNativeMethods.GetDpiForMonitor(monitor, MonitorDpiType.MDT_EFFECTIVE_DPI, out dpiX, out dpiY); @@ -42,6 +42,12 @@ public static double GetScaleRatioForWindow(Window This) return ((double)dpiX) / wpfDpi; } } + + public static double GetScaleRatioForWindow(Window This) + { + var hwndSource = PresentationSource.FromVisual(This) as HwndSource; + return GetScaleRatioForWindow(hwndSource.Handle); + } } public class PerMonitorDpiBehavior