我一直在尝试使用 <Windows.h> 的函数获取屏幕尺寸GetDeviceCaps(GetDC(NULL), HORZRES),但每当我运行代码时,它总是返回屏幕分辨率的一半。
有谁知道为什么我的电脑会发生这种情况?它在大多数其他显示器上运行良好。
我的屏幕分辨率是 (2736x1824) (surface pro)。
#include <Windows.h>
#include <iostream>
int main()
{
HDC display = GetDC(NULL);
const int x = GetDeviceCaps(display, HORZRES), y = GetDeviceCaps(display, VERTRES); //returns (1368, 912)
std::cout << x << ", " << y << "\n";
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您的程序几乎肯定“遭受”了DPI 感知问题。
在我的系统上运行您的代码会出现类似的问题;但是,添加对该SetThreadDpiAwarenessContext函数的调用可以解决该问题:
#include <Windows.h>
#include <iostream>
int main()
{
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE); // This line fixes the issue.
HDC display = GetDC(NULL);
const int x = GetDeviceCaps(display, HORZRES), y = GetDeviceCaps(display, VERTRES); //returns (1368, 912)
std::cout << x << ", " << y << "\n";
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果没有添加该调用,程序将显示“1536, 864”的输出。添加后,我看到(正确的)值:“1920, 1080”。
| 归档时间: |
|
| 查看次数: |
950 次 |
| 最近记录: |