GetSystemMetrics() 返回 SM_CXSCREEN 的错误值
我遇到了一个有趣的问题。至少在 Vista 中,当桌面 DPI 设置未设置为 100% 时,getSystemMetrics(SM_CXSCREEN) 将返回不正确的值。例如,我在 1366x768 屏幕中尝试了 150%,getSystemMetrics() 返回 911 而不是 1366(和 1366 / 1.5 ~ 911)
根据 MSDN,getSystemMetrics(SM_CXSCREEN) 返回像素,所以我认为这个值不会受到影响通过 DPI 设置 - 但确实如此。那么有没有更安全的方法来找出真实的、未缩放的屏幕分辨率呢?
I've run into an interesting problem. At least in Vista, getSystemMetrics(SM_CXSCREEN) returns an incorrect value when the desktop DPI settings aren't set at 100%. For example, I tried 150% in a 1366x768 screen and getSystemMetrics() returns 911 instead of 1366 (and 1366 / 1.5 ~ 911)
According to the MSDN, getSystemMetrics(SM_CXSCREEN) returns pixels, so I thought this value wouldn't be affected by the DPI settings - but it is. So is there a safer way to find out the true, unscaled screen resolution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当超过 125% 时,程序必须告诉操作系统它具有 DPI 感知能力才能获得真正的分辨率。最好使用清单来完成,如 MSDN Library 中所述文章。
A program must tell the operating system that it is DPI-aware to get the true resolution when you go past 125%. That's best done with a manifest, as explained in this MSDN Library article.
为了让您的应用程序了解 dpi make 和清单文件,并将以下文本放入其中。
To make you application aware of dpi make and manifest file and put the following text in it.
在从 GetSystemMetrics 或 GetClientRect 等函数获取正确的像素指标之前,您的应用程序必须向操作系统声明它了解 DPI,因此不会搞砸一切。
其建议方式发生了一些变化。有关更多详细信息,请参阅 MSDN 文档。
从 Windows 10 开始:
或从 Windows 8.1 开始:
或从 Vista 开始:
调用此函数后,函数 GetSystemMetrics 等应以像素为单位返回正确的值到您的应用程序。
Before getting the correct pixel metrics from functions like GetSystemMetrics, or GetClientRect, your app must declare to the OS that it knows about DPI and therefore won't screw everything up.
There have been several changes to how this is recommended. See the MSDN docs for more details.
From Windows 10 onwards:
Or from Windows 8.1 onwards:
Or from Vista onwards:
After calling this, the function GetSystemMetrics etc. should return the correct values to your app in pixels.