为什么 "GetDeviceCaps" 函数总是 return 恰好是我屏幕尺寸的一半?

Why does the "GetDeviceCaps" Function always return exactly half of the size of my screen?

我一直在尝试使用 GetDeviceCaps(GetDC(NULL), HORZRES) 函数获取屏幕尺寸,但是每当我 运行 代码时,它总是 returns恰好是我屏幕分辨率的一半。

有谁知道为什么我的电脑会出现这种情况?它在大多数其他显示器上工作正常。

我的屏幕分辨率是 (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;
}

您的程序几乎可以肯定是 'suffering' 来自 DPI Awareness issues

运行 你的代码在我的系统上出现了类似的问题;但是,添加对 SetThreadDpiAwarenessContext function 的调用可以解决问题:

#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;
}

如果没有添加调用,程序会显示“1536, 864”的输出。添加后,我看到了(正确的)值:“1920、1080”。