/////////////////

如今显示器的分辨率是越来越大,2K,4K分辨率的都很常见了。在高分屏下,我们必须对DPI进行缩放,要不显示字体会很小。一般系统默认DPI缩放级别为100%。如果改成150%,200%或者其他,我们会发现部分程序界面显示错乱,这是因为这些程序没有对高DPI进行处理。在VS中编写程序时可以很容易的做到支持DPI。

第一种方法是修改程序的Mainfest,如下图,我用的是VS2015,在属性页的Mainfest Tool中进行修改。
VC,高DPI支持 & c++ 获取屏幕显示比例

第二种方法是使用SetProcessDpiAwareness函数:

传入的参数有三个取值选择:

对应前面VS设置中的三个选项,具体使用可以参考SetProcessDpiAwareness function


//////////////////////////

VC,高DPI支持 & c++ 获取屏幕显示比例

屏幕默认的显示比例是100%,但是有些用户喜欢调到“中等”或“较大”,这样的话,就可能导致我们开发的应用超出屏幕边界。因此,我们要获取屏幕显示比例,做出相应的处理。

Primary display DPI scale factor

Similarly, you can get the pixel density by using the LOGPIXELSX and LOGPIXELSY indices:

syntax
[cpp] view plain copy
  1. // Get desktop dc  
  2. desktopDc = GetDC(NULL);  
  3. // Get native resolution  
  4. horizontalDPI = GetDeviceCaps(desktopDc,LOGPIXELSX);  
  5. verticalDPI = GetDeviceCaps(desktopDc,LOGPIXELSY);  

These results are returned in a coordinate system in which 96 corresponds to 100%, as shown inTable 2 DPI Scale Factors.

VC,高DPI支持 & c++ 获取屏幕显示比例


参考链接:https://docs.microsoft.com/zh-cn/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings




VC,高DPI支持 & c++ 获取屏幕显示比例

屏幕默认的显示比例是100%,但是有些用户喜欢调到“中等”或“较大”,这样的话,就可能导致我们开发的应用超出屏幕边界。因此,我们要获取屏幕显示比例,做出相应的处理。

Primary display DPI scale factor

Similarly, you can get the pixel density by using the LOGPIXELSX and LOGPIXELSY indices:

syntax
[cpp] view plain copy
  1. // Get desktop dc  
  2. desktopDc = GetDC(NULL);  
  3. // Get native resolution  
  4. horizontalDPI = GetDeviceCaps(desktopDc,LOGPIXELSX);  
  5. verticalDPI = GetDeviceCaps(desktopDc,LOGPIXELSY);  

These results are returned in a coordinate system in which 96 corresponds to 100%, as shown inTable 2 DPI Scale Factors.

VC,高DPI支持 & c++ 获取屏幕显示比例


参考链接:https://docs.microsoft.com/zh-cn/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings

相关文章:

  • 2021-10-22
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2022-02-22
  • 2022-01-07
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2021-12-10
  • 2021-07-03
  • 2022-12-23
  • 2021-09-19
相关资源
相似解决方案