简体   繁体   中英

How to get the scale factor to obtain the real screen resolution?

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM