9
votes

On Windows 10, the system no longer requires restart (logoff/logon) to change DPI (text size).

My application is not ready for such a runtime change.

How does system inform an application that the DPI is changing? What message should I handle?


I'm using C++ Builder, so I'm looking for a pure WinAPI solution, no WinForms.

2

2 Answers

10
votes

As @TLama and @DalijaPrasnikar commented, the message to handle is WM_DPICHANGED.

#define WM_DPICHANGED       0x02E0

The message is available since Windows 8.1, where it is sent to a window when the window is moved to another monitor with a different DPI.

On Windows 10, the runtime change of DPI is, from an API point of view, identical to moving window to a different monitor with different DPI (except of course that in this case the window stays on the same monitor).

To even receive the message (even for runtime DPI change on Windows 10), the application needs to declare support for per-monitor DPI. The preferred way is to declare the support in an application manifest by setting dpiAwareness to PerMonitorV2 (and additional PerMonitor value and set dpiAware to True/PM for backward compatibility with older Windows 10 builds and Windows 8.1):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    ...
    <asmv3:application>
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
            <dpiAware>True/PM</dpiAware>
            <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2,PerMonitor</dpiAwareness>
        </asmv3:windowsSettings>
    </asmv3:application>
</assembly>

When the application declares per-monitor DPI support, the system won't scale its windows pixel-wise on DPI runtime change. It's then up to the application to scale its windows natively.

1
votes

Since C++ Builder 10.3 you can use these form events:

  • OnAfterMonitorDpiChanged
  • OnBeforeMonitorDpiChanged

Of course you also need to set DPI Awareness to PerMonitorV2 in the application manifest in the project settings.