0

I'm trying to get the screen resolution scale factor to obtain the correct screen resolution. I tried different scale factor combinations but the 'dsf' variable is always set to 100. How can I fix this?

RECT System::GetScreenResolution(HWND hwnd)
{
    //get resolution scale factor
    HMONITOR hmonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY);
    DEVICE_SCALE_FACTOR dsf;
    GetScaleFactorForMonitor(hmonitor, &dsf);

    //get and calculate real resolution
    HDC hdc = GetDC(hwnd);
    
    RECT r{ 0, 0, GetDeviceCaps(hdc, HORZRES) * dsf / 100, GetDeviceCaps(hdc, VERTRES) * dsf / 100 };

    ReleaseDC(hwnd, hdc);

    return r;
}

I build the program under Windows 10 64bit using Visual Studio 2019 and I'm using multiple monitor with extended display. Thanks in advance.

Caner Kurt
  • 91
  • 1
  • 9
  • Does this answer your question? [How to get the Monitor Screen Resolution from a hWnd?](https://stackoverflow.com/questions/2156212/how-to-get-the-monitor-screen-resolution-from-a-hwnd) – MartinVeronneau Nov 06 '20 at 15:57
  • I can get the screen resolution, my problem is I can't get the correct scaling factor. Windows API function always sets the variable to '100' no matter what I select from the display settings. – Caner Kurt Nov 06 '20 at 16:06
  • You might wanna check that answer : https://stackoverflow.com/a/36864741/5688187 – MartinVeronneau Nov 06 '20 at 16:54
  • I have two monitors (one 100% the other 150%). and it works fine for me. Maybe you should try to enumerate all monitors with EnumDisplayMonitors instead of relying on a window. And BTW, the screen final size (depending on scale factor) can be get using MONITORINFO.rcMonitor (GetMonitorInfo). – Simon Mourier Nov 06 '20 at 19:29
  • You need to mark your process as DPI aware, otherwise the OS lies to you. – Jonathan Potter Nov 06 '20 at 21:06

1 Answers1

0

For GetScaleFactorForMonitor() to work accurately under Windows 10, you need to have a manifest with a corresponding supported OS id...

Note that, with dpiAware = true in the manifest, GetDeviceCaps(hdc, HORZRES) will give valid result on application start but erroneous result after a dynamic scale change – so read it on start and keep this value.

   <!-- Compatibility section for Program Compatibility Assistant (PCA) -->
   <compatibility xmlns='urn:schemas-microsoft-com:compatibility.v1'>
     <application>
       <supportedOS Id='{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}'/>
     </application>
   </compatibility>
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83