简体   繁体   English

如何获取比例因子以获得真实的屏幕分辨率?

[英]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?我尝试了不同的比例因子组合,但“dsf”变量始终设置为 100。我该如何解决这个问题?

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.我使用 Visual Studio 2019 在 Windows 10 64 位下构建程序,并且我使用具有扩展显示的多显示器。 Thanks in advance.提前致谢。

For GetScaleFactorForMonitor() to work accurately under Windows 10, you need to have a manifest with a corresponding supported OS id...为了让GetScaleFactorForMonitor()在 Windows 10 下准确工作,您需要有一个包含相应支持的操作系统 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.请注意,如果清单中的dpiAware = trueGetDeviceCaps(hdc, HORZRES)将在应用程序启动时给出有效结果,但在动态比例更改后给出错误结果——因此在启动时读取并保留此值。

   <!-- 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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