About DPI issue

安稳与你 提交于 2019-12-02 03:22:17

问题


I have a WIN32 SW which the UI was designed in 96 DPI, so when user changes the windows DPI from 96 to 120 or bigger, the UI will be wrong. I want to know if there is API to force my SW to display the UI with 96DPI.


回答1:


Starting with Windows Vista, scaling for DPI is supposed to happen automatically. I don't have any direct experience to know how well it works, but here's the page that explains how to turn it off:

http://msdn.microsoft.com/en-us/library/ms701681(VS.85).aspx




回答2:


You can also add an appcompat key for your application. The place for this in the registry is:

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

That is the per-user settings, there is the same key in HKEY_LOCAL_MACHINE, but of course that is a system setting and will require elevated privileges to write to. Adding a key like so:

"C:\path\to\app.exe"="HIGHDPIAWARE"

Will enable that compatibility flag for your program, which will turn off DPI scaling. This is for Vista+.

SetProcessDPIAware is also an option, but be aware there is a danger of a race condition, according to the documentation.




回答3:


There is no API to force your app to show at 96DPI. The DPI is a device setting and cannot be controlled per application.

If you can change your program, you can scale your UI to look properly on high DPI though. You need to call GetDeviceCaps; more specificaly, you need to calculate the X and Y scale using the number returned for LOGPIXELSX and LOGPIXELSY. Something like this:

HDC hdc;
double m_dDPIScaleX = GetDeviceCaps(hdc, LOGPIXELSX) / 96.0;
double m_dDPIScaleY = GetDeviceCaps(hdc, LOGPIXELSY) / 96.0;


来源:https://stackoverflow.com/questions/572270/about-dpi-issue

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