Handle runtime change of DPI (text size) on Windows 10

我怕爱的太早我们不能终老 提交于 2019-12-06 21:41:25

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31702495/handle-runtime-change-of-dpi-text-size-on-windows-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!