5

I am making a call through interop to GetScaleFactorForMonitor() on a Windows8.1 Surface3 tablet. It responds with a scaling factor of 140. I only have one monitor on this tablet. I set the flag for this call to both nearest to window and primary display to see if any difference just for the hell of it and both give 140.

The issue is, the actual scaling is 150%. When I go to view display settings in Windows it says 1440x2160 but the native resolution is 960x1440 (which I got from calling Screen.PrimaryScreen.Bounds).

I also get 960x1440 when calling SystemInformation.PrimaryMonitorSize.Height and Width.

I explored another route trying to use GetDpiForMonitor(). My thinking is if I get the raw DPI and then the scaled DPI I could do a percentage calculation.

It sort of works but the value I get for the raw DPI seems to be the effective(scaled) DPI and visa versa. For effective DPI for both x and y I get 96 and for raw I get 144. I would expect the raw to be a lower number.

Here are my calls:

GetDpiForMonitor(MonitorFromWindow(myTextbox.TopLevelControl.Handle, MONITOR_DEFAULTTONEAREST),
                                   MONITOR_DPI_TYPE.MDT_Effective_DPI,
                                   out effectiveDPIx,
                                   out effectiveDPIy);
GetDpiForMonitor(MonitorFromWindow(myTextbox.TopLevelControl.Handle, MONITOR_DEFAULTTONEAREST),
                                   MONITOR_DPI_TYPE.MDT_Raw_DPI,
                                   out rawDPIx,
                                   out rawDPIy);

Here is the struct I am using that is a mirror of the one on MSDN.

public enum MONITOR_DPI_TYPE : int
{
    MDT_Effective_DPI = 0,
    MDT_Angular_DPI = 1,
    MDT_Raw_DPI = 2,
    MDT_Default = MDT_Effective_DPI
};
2
  • 1
    I have no idea why GetScaleFactorForMonitor does not return the correct scaling factor. I can repro the exact behavior that you describe. When I have a 150% scale applied (144 DPI on the test monitor, compared to a 96 system DPI), GetScaleFactorForMonitor returns 140. One would obviously expect it to return 150, since 144/96=1.5. As for your second question, I suspect it's related to the fact that you haven't included a manifest in your application that specifies you are per-monitor high DPI compatible. See: msdn.microsoft.com/en-us/library/windows/desktop/…
    – Cody Gray
    Apr 26, 2016 at 9:03
  • 2
    The only thing I can think of is that GetScaleFactorForMonitor is based on the Windows Store scaling modes, which are actually different than those for desktop applications. (See blogs.technet.microsoft.com/askcore/2015/12/08/…, in particular the section named "Unified and extended scaling system".) Your best bet is to use GetDpiForMonitor. It's too bad the per-monitor scaling support is utterly broken, even in the latest version Windows 10. The Windows team is delivering nothing but disappointment these days.
    – Cody Gray
    Apr 26, 2016 at 9:10

3 Answers 3

2

I realise now that this is old, but it's worth pointing out why this appears to be happening.

The comment from Cody Gray is a good starting point. The link at https://blogs.technet.microsoft.com/askcore/2015/12/08/display-scaling-in-windows-10/ under the heading "Unified and extended scaling system" does show that Windows 10 has unified the scales across all APIs.

However, the link https://learn.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows (also from Cody Gray) indicates that it isn't actually the case that Windows 10 has unified it, at least not if you're on an older Windows version.

The heading "Per Monitor DPI Scaling Support by UI Framework" shows that e.g. Win32 will only return the correct value for version 1703 (Creator's Update), while UWP will use the unified scale if using Windows versions 1607+.

Earlier versions of Windows return the same values if one had called GetScaleFactorForDevice (i.e. 100, 140 or 180).

0

The numbers you've quoted for a Surface Pro do look wrong but when I run the same test on mine I get a raw DPI of 216 (which is correct). Are you sure you're asking for the RAW DPI?

MDT_RAW_DPI returns the physical dpi of the monitor - so a different sized monitor at the same resolution will give a different answer.

eg: my main monitor has a physical width of ‪23.5" and horizontal resolution of 3840. 3840 / 23.5 = 163 dpi

eg: surface pro 3 has a physical width of ~10" and resolution of 2160. 2160 / 10 = 216 dpi.

Both of these match what I'm getting from MDT_RAW_DPI running on latest Windows 10 (perhaps they've fixed something?)

Here's my test program:

https://gist.github.com/toptensoftware/a6b8ca2cfc9ac63e7b6687968db408a2

0

It turned out that this depends on the dpi awareness of the calling process. GetScaleFactorForMonitor() returns the correct result if the calling application has set its dpi awareness to DPI_AWARENESS_UNAWARE.

If any of the others (DPI_AWARENESS_SYSTEM_AWARE, DPI_AWARENESS_PER_MONITOR_AWARE) is set, then the results are incorrect.

The other problem is, that GetDpiForMonitor or GetDpiForWindow work only correct if the calling process has DPI_AWARENESS_PER_MONITOR_AWARE. See: https://learn.microsoft.com/en-us/windows/win32/api/shellscalingapi/nf-shellscalingapi-getdpiformonitor

If you define your program for running on Windows 10 or higher you can determine the awareness programmatically.

#if WINVER >= 0x0A00
    DPI_AWARENESS_CONTEXT oldContext = GetThreadDpiAwarenessContext();
    DPI_AWARENESS oldDpiAwareness = GetAwarenessFromDpiAwarenessContext(oldContext);
#endif

If not, there seems to be no way to get the correct DPI setting (or scaling factor) for the monitor you are running on.

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.