1

(No, I can't upgrade this codebase to .NET Core or newer yet.)

I have an app that's primarily Windows Forms, and partially WPF. When font size changes at runtime, WPF reacts correctly. Windows Forms, instead, doesn't even seem to realize anything has changed unless I restart. For example, the Control.DeviceDpi property (which wasn't even introduced until 4.7) shows the value that was accurate at startup, not the current one.

My app.manifest has:

  <asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
      <dpiAware>True/PM</dpiAware>
    </asmv3:windowsSettings>
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
      <dpiAwareness>PerMonitorV2, PerMonitor</dpiAwareness>
    </asmv3:windowsSettings>
  </asmv3:application>

My app.config has:

    <appSettings>
        <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
    </appSettings>

But 4.7 did introduce a Control.DpiChanged event, only it never seems to fire.

Yet, if I override WndProc and listen to WM_DPICHANGED, that does fire. This code seems to suggest that the event should fire.

Apparently, it doesn't because this code conditionally turns it off. But I'm not sure which portion I don't qualify for. (I'm on Windows 10 21H1, so more recent than RS2.)

Sören Kuklau
  • 19,454
  • 7
  • 52
  • 86
  • 1
    Use [.Net Framework 4.8](https://referencesource.microsoft.com/#System.Windows.Forms/misc/DpiHelper.cs,63) and move PerMonitorV2 settings to `app.config`. It only works in Windows 10 (since this calls [GetThreadDpiAwarenessContext](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getthreaddpiawarenesscontext)). -- WPF-style auto-resizing is... a work in progress :) – Jimi Jul 08 '21 at 13:17
  • 3
    DpiChanged is formally undocumented. It has even been removed from the reference source index, they really meant it. No real point in trying to use it anyway, favor the documented events instead (DpiChangedBefore/AfterParent). Win10 version 1703+ (April 2017) required. – Hans Passant Jul 08 '21 at 15:06

0 Answers0