GetSystemMetrics() returns wrong value for SM_CXSCREEN

后端 未结 3 1927
悲哀的现实
悲哀的现实 2020-12-03 18:05

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 exa

3条回答
  •  一向
    一向 (楼主)
    2020-12-03 18:57

    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:

    ::SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
    

    Or from Windows 8.1 onwards:

    ::SetProcessDpiAwareness(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
    

    Or from Vista onwards:

    ::SetProcessDPIAware();
    

    After calling this, the function GetSystemMetrics etc. should return the correct values to your app in pixels.

提交回复
热议问题