【问题标题】:How to get the scale factor to obtain the real screen resolution?如何获取比例因子以获得真实的屏幕分辨率?
【发布时间】:2021-09-18 02:57:51
【问题描述】:

我正在尝试获取屏幕分辨率比例因子以获得正确的屏幕分辨率。我尝试了不同的比例因子组合,但“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;
}

我使用 Visual Studio 2019 在 Windows 10 64 位下构建程序,并且我使用的是带扩展显示的多显示器。 提前致谢。

【问题讨论】:

  • 我可以得到屏幕分辨率,我的问题是我无法得到正确的缩放因子。无论我从显示设置中选择什么,Windows API 函数总是将变量设置为“100”。
  • 你可能想检查那个答案:stackoverflow.com/a/36864741/5688187
  • 我有两台显示器(一台 100%,另一台 150%)。它对我来说很好。也许您应该尝试使用 EnumDisplayMonitors 枚举所有监视器,而不是依赖窗口。顺便说一句,屏幕最终尺寸(取决于比例因子)可以使用 MONITORINFO.rcMonitor (GetMonitorInfo) 获得。
  • 您需要将您的进程标记为 DPI 感知,否则操作系统会欺骗您。

标签: c++ windows winapi


【解决方案1】:

要让GetScaleFactorForMonitor() 在 Windows 10 下正常工作,您需要有一个具有相应支持的 OS id 的清单...

请注意,dpiAware = true 在清单中,GetDeviceCaps(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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 2013-09-30
    • 2011-04-10
    相关资源
    最近更新 更多