Skip to content

Commit

Permalink
Allow querying scale ratio for arbitrary hwnds
Browse files Browse the repository at this point in the history
  • Loading branch information
anaisbetts committed Apr 20, 2015
1 parent 54afc54 commit 234a4ea
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions PerMonitorDpiBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,31 @@ 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);

return ((double)dpiX) / wpfDpi;
}
}

public static double GetScaleRatioForWindow(Window This)
{
var hwndSource = PresentationSource.FromVisual(This) as HwndSource;
return GetScaleRatioForWindow(hwndSource.Handle);
}
}

public class PerMonitorDpiBehavior
Expand Down

0 comments on commit 234a4ea

Please sign in to comment.