GetSystemMetrics为我的屏幕返回错误的值

GetSystemMetrics return wrong value for my screen

本文关键字:错误 返回 屏幕 我的 GetSystemMetrics      更新时间:2023-10-16

我有一个示例代码来获取像素的高度和宽度。但是,它显示错误的分辨率为我的屏幕。

我有3840 x 2160。

但是应用程序显示:1920 × 1080。

代码:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int CDECL MessageBoxPrintf(TCHAR * szCaption, TCHAR * szFormat, int in, int in2)
{
    TCHAR szBuffer[1024];
    va_list pArgList;
    // The va_start macro (defined in STDARG.H) is usually equivalent to
    // pArgList = (char *) &szFormat + sizeof (szFormat) ;
    va_start(pArgList, szFormat);
    // The last argument to wvsprintf points to the arguments
    _vsntprintf_s(szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
    szFormat, pArgList);
    // The va_end macro just zeroes out pArgList for no good reason
    va_end(pArgList);
    return MessageBox(NULL, szBuffer, szCaption, 0);
}
int WINAPI WinMain(HINSTANCE hTnstance, HINSTANCE hPreVInStane,PSTR szCmdLine, int iCmdShow)
{
    int cxScreen=0, cyScreen=0;
    cxScreen = GetSystemMetrics(SM_CXSCREEN);
    cyScreen = GetSystemMetrics(SM_CYSCREEN);
    MessageBoxPrintf(TEXT("ScrnSize"), TEXT("Wide %i High %i"), cxScreen, cyScreen);
    return 0;
}  

这个函数是过时的(GetSystemMetrics)还是我做错了什么?

问题是由于您的显示DPI/缩放设置

请参见GetSystemMetrics()返回SM_CXSCREEN的错误值

还有SetProcessDPIAware和SetProcessDpiAwareness API