DPI 百分比缩放到实际 DPI 值

DPI Percentage Scaling to actual DPI value

我们知道 Windows 7 种可用的 DPI 缩放比例为 100%、125%、150% 和 200%。 这四个 DPI 百分比的实际 DPI 值为

Percentage - DPI Values
100%       - 96
125%       - 120
150%       - 144
200%       - 192

DPI 缩放参考 link: http://www.techrepublic.com/blog/windows-and-office/get-a-better-view-in-windows-7-by-adjusting-dpi-scaling/

我想使用 C# 获取 DPI 值。因此,通过遵循 C# 代码,我试图实现。

float x=0;
float y=0;
Graphics gp = Graphics.FromHwnd(IntPtr.Zero);// we can also use this.CreateGraphics()
x = gp.DpiX;
y = gp.DpiY;

得到如下输出,150% 和 200% 是错误的

100%  -  96 //both x,y values
125%  - 120 //both x,y values
**150%  -  96 //both x,y values
200%  -  96 //both x,y values**

如果您没有将您的应用程序声明为 "DPI aware",Windows 会骗您,假装它设置为 96 DPI(尽管它不是)并注意缩放您的应用程序本身。

您可以"fix"通过

  • 向您的应用程序清单文件添加 dpiAware 条目或
  • 正在调用 SetProcessDPIAware Windows API 方法。

可以找到两者的示例,例如,在这个 SO 答案中:

  • How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)? - Answer by Hans Passant

为了让事情变得更加复杂,Windows 8.1 引入了 per-monitor DPI 设置,弃用了 SetProcessDPIAware。这其实是一件好事,但是it's hard to get it right.