Windows Dpi 缩放

MSDN:

1.  设置当前进程或者线程对dpi设置的感知

SetProcessDpiAwarenessContext function (winuser.h) - Win32 apps | Microsoft DocsSets the current process to a specified dots per inch (dpi) awareness context. The DPI awareness contexts are from the DPI_AWARENESS_CONTEXT value.https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiawarenesscontextSetThreadDpiAwarenessContext function (winuser.h) - Win32 apps | Microsoft Docshttps://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-setthreaddpiawarenesscontext2. 获取当前系统dpiGetDpiForSystem function (winuser.h) - Win32 apps | Microsoft DocsReturns the system DPI.https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdpiforsystem3. 接收dpi变更通知
WM_DPICHANGED WinUser.h (的消息) - Win32 apps | Microsoft Docshttps://docs.microsoft.com/zh-cn/windows/win32/hidpi/wm-dpichanged

说明:

1. 设置进程或者线程dpi不同点在于,dpi对ui元素生效的范围不同

2. 

The return value will be dependent based upon the calling context. If the current thread has a DPI_AWARENESS value of DPI_AWARENESS_UNAWARE, the return value will be 96. That is because the current context always assumes a DPI of 96. For any other DPI_AWARENESS value, the return value will be the actual system DPI.

You should not cache the system DPI, but should use GetDpiForSystem whenever you need the system DPI value.

3. 要接收系统dpi变化消息,需要设置dpi感知为:

DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE 或者

DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2

复杂的用法可以在接收到通知之后重新用SetWindowsPos设置UI中元素的大小,简单的方式就是在收到dpi改变的消息后询问用户是否重启应用以适配新的dpi设置

 

附:

DPI 几种感知模式的不同:

另外QT 5.6 之后就已经支持对DPI缩放的支持, 在 QApplication 构造之前设置

QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

就能自动适应不同 dpi 的调整,Qt 6.0 之后已经不提供开关,默认自适应生效。


版权声明:本文为linfengmove原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。