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

前端 未结 2 683
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 07:28

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 doe

2条回答
  •  甜味超标
    2021-01-02 07:57

    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):

    
    
        ...
        
            
                True/PM
                PerMonitorV2,PerMonitor
            
        
    
    

    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.

提交回复
热议问题