14

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 example, I tried 150% in a 1366x768 screen and getSystemMetrics() returns 911 instead of 1366 (and 1366 / 1.5 ~ 911)

According to the MSDN, getSystemMetrics(SM_CXSCREEN) returns pixels, so I thought this value wouldn't be affected by the DPI settings - but it is. So is there a safer way to find out the true, unscaled screen resolution?

2
  • From what kind of application are you calling this? I just tried calling that API from C and ran it on Vista Business at different DPI settings and different resolutions and it reported the pixels correctly in each instance. It is a vmware image, so maybe that causes a difference. Apr 13, 2010 at 15:23
  • It's a regular, plain C++ app. No standard frameworks, just WinMain.
    – ggambetta
    Apr 14, 2010 at 19:31

3 Answers 3

13

A program must tell the operating system that it is DPI-aware to get the true resolution when you go past 125%. That's best done with a manifest, as explained in this MSDN Library article.

1
  • The 125% is indeed the breakpoint after which Windows API starts returning wrong coordinates. Once you change scaling from 125 to 126, things start to get messy. But where does this number come from? Is it documented anywhere? yesterday
8

To make you application aware of dpi make and manifest file and put the following text in it.

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
  <asmv3:application>
    <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
      <dpiAware>true</dpiAware>
    </asmv3:windowsSettings>
  </asmv3:application>
</assembly>
6

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.

1
  • 1
    It's always recommended that you use the manifest to set your process' DPI awareness. There are three types of DPI awareness: 1) unaware (you think that the world is always 96 DPI) 2) system aware (you understand DPI but think that the world is always the system DPI and will never change) 3) per-monitor aware (you understand DPI and that it can change while you're running) SetProcessDPIAware() makes your entire process system aware SetProcessDpiAwareness(...) lets you specify the awareness mode of your process SetThreadDpiAwarenessContext(...) enables sub-process DPI awareness
    – peterfelts
    Mar 16, 2017 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.