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

Viewed 347

I've been trying to get the screen size with <Windows.h>'s GetDeviceCaps(GetDC(NULL), HORZRES) function, but whenever I run the code, it always returns exactly half of my screen resolution.

Does anyone know why this may happen to my computer? It works fine on most other monitors.

My screen resolution is (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;
}
1 Answers

Your program is almost certainly 'suffering' from DPI Awareness issues.

Running your code on my system presents similar issues; however, adding a call to the SetThreadDpiAwarenessContext function resolves the problem:

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

Without that added call, the program shows output of "1536, 864". With it added, I see the (correct) values: "1920, 1080".

Related