void AssociatedObject_SourceInitialized(object sender, EventArgs e) { AddHwndHook(); currentDpiRatio = MonitorDpi.GetScaleRatioForWindow(AssociatedObject); UpdateDpiScaling(currentDpiRatio); }
static PerMonitorDpiBehavior() { if (MonitorDpi.IsHighDpiMethodSupported()) { // NB: We need to call this early before we start doing any // fiddling with window coordinates / geometry SafeNativeMethods.SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE); } }
IntPtr HwndHook(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (message) { case NativeConstants.WM_DPICHANGED: var rect = (RECT)Marshal.PtrToStructure(lParam, typeof(RECT)); SafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SetWindowPosFlags.DoNotChangeOwnerZOrder | SetWindowPosFlags.DoNotActivate | SetWindowPosFlags.IgnoreZOrder); var newDpiRatio = MonitorDpi.GetScaleRatioForWindow(AssociatedObject); if (newDpiRatio != currentDpiRatio) { UpdateDpiScaling(newDpiRatio); } break; } return(IntPtr.Zero); }