1

我最近一直在使用 WINAPI 来检索缩放设置和屏幕分辨率,我遇到了这种奇怪的行为。

我有一个小的 C++ 程序(下面有列表),它可以检索 VERTRES、HORZRES、DESKTOPVERTRES 和 DESKTOPHORZRES 的值。我的程序还将 DPI 感知模式设置为 1(系统级感知)。我注意到 VERTRES、HORZRES 报告的值取决于系统启动时使用的系统比例因子。

所以在我的笔记本电脑上,我将系统缩放系数配置为 150%,分辨率为 1920x1080。那是启动时的配置。当我检索 HORZRES 和 DESKTOPHORZRES 时,两个值都报告为 1920。

如果我将缩放设置更改为 100% 并且不重新启动计算机,则下次检索这些值时,它们会报告为 2880(对于 HORZRES)和 1920(对于 DESKTOPHORZRES)。

在我重新启动计算机并将缩放设置设置为 100% 后,这两个值再次报告为 1920。

如果我再次将缩放比例更改为 150%,则值将报告为 1280 (HORZRES) 和 1920 (DESKTOPHORZRES)。

仅当我将 DPI 感知设置为 1 时才观察到所描述的行为,如果它设置为 0(“不感知”)或 2(“每屏幕感知”)值始终报告为 1280(HORZRES)和 1920(DESKTOPHORZRES)无论在启动时如何配置缩放。

我想知道为什么 HORZRES(或 VERTRES)报告的值取决于系统启动时使用的比例因子?这是预期的行为吗?

如果上面已经在某处进行了描述,我将不胜感激任何参考。

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cerrno>
#include <string>
#include <sstream>
#include <math.h>
#include <Windows.h>
#include <shellscalingapi.h>
#include <winuser.h>

class Rectangular
{
public:
    Rectangular(int height, int width) : height(height), width(width) {};
    const int height;
    const int width;
};

class MonitorInfo
{
public:
    MonitorInfo(std::string device_name, Rectangular logical_resolution, Rectangular physical_resolution, Rectangular physical_size_mm) :
        device_name(device_name), logical_resolution(logical_resolution), physical_resolution(physical_resolution),
        physical_size_mm(physical_size_mm), scaling(static_cast<int>(std::round(100.0*(float)physical_resolution.height / (float)logical_resolution.height))) {};

    void print_to_stdout() const;
    const std::string device_name;
    const Rectangular logical_resolution;
    const Rectangular physical_resolution;
    const Rectangular physical_size_mm;
    const int scaling;
};


class MonitorsInformation
{
public:
    MonitorsInformation();
    const std::vector<MonitorInfo>& get_monitors_info() const { return monitors; }
    std::string get_last_error_string() const { return last_error; }
    Rectangular get_monitors_rectangular() const;
private:
    RECT rectangular_combined;
    std::vector<MonitorInfo> monitors;
    std::string  last_error;
    static BOOL CALLBACK MonitorEnum(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData);
};

void MonitorInfo::print_to_stdout() const
{
    std::cout << "\nDevice: " << device_name << "\nLogical Screen resolution: " << logical_resolution.width << "x" << logical_resolution.height;
    std::cout << "\nPhysical Screen resolution: " << physical_resolution.width << "x" << physical_resolution.height;
    std::cout << "\nDPI ratio: " << scaling;
    std::cout << "\nPhysical Size(mm): " << physical_size_mm.width << "x" << physical_size_mm.height << "\n";
}


MonitorsInformation::MonitorsInformation() : rectangular_combined(RECT()), last_error(std::string()), monitors(std::vector<MonitorInfo>())
{
    EnumDisplayMonitors(0, 0, MonitorEnum, (LPARAM)this);
}

BOOL CALLBACK MonitorsInformation::MonitorEnum(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData)
{
    MonitorsInformation* pThis = reinterpret_cast<MonitorsInformation*>(pData);

    MONITORINFOEXA monitor_info;
    monitor_info.cbSize = sizeof(MONITORINFOEXA);

    if (!GetMonitorInfoA(hMon, &monitor_info))
    {
        pThis->last_error = "GetMonitorInfoA failed with error: " + errno;
        return FALSE;
    }

    UnionRect(&pThis->rectangular_combined, &pThis->rectangular_combined, lprcMonitor);

    HDC device_context = CreateDCA(NULL, monitor_info.szDevice, NULL, NULL);

    int LogicalScreenHeight = GetDeviceCaps(device_context, VERTRES);
    int LogicalScreenWidth = GetDeviceCaps(device_context, HORZRES);
    int PhysicalScreenHeight = GetDeviceCaps(device_context, DESKTOPVERTRES);
    int PhysicalScreenWidth = GetDeviceCaps(device_context, DESKTOPHORZRES);

    pThis->monitors.push_back(
        MonitorInfo(
            std::string(monitor_info.szDevice),
            Rectangular(
                LogicalScreenHeight,
                LogicalScreenWidth),
            Rectangular(
                PhysicalScreenHeight,
                PhysicalScreenWidth),
            Rectangular(
                GetDeviceCaps(device_context, VERTSIZE),
                GetDeviceCaps(device_context, HORZSIZE))));

    return TRUE;
}

Rectangular MonitorsInformation::get_monitors_rectangular() const
{
    return Rectangular(
        std::abs(rectangular_combined.top) + std::abs(rectangular_combined.bottom),
        std::abs(rectangular_combined.left) + std::abs(rectangular_combined.right));
}


int main()
{
    SetProcessDPIAware();

    for (;;)
    {
        MonitorsInformation MonitorsInfo;
        char exit_char = 'N';
        std::cout << "You have " << MonitorsInfo.get_monitors_info().size() << " monitors connected.\n";
        printf("Screen rectangular. %d x %d\n",
            MonitorsInfo.get_monitors_rectangular().width, MonitorsInfo.get_monitors_rectangular().height);

        for (auto &monitor : MonitorsInfo.get_monitors_info())
        {
            monitor.print_to_stdout();
        }
        std::cout << "Would you like to repeat? [Y]/[N]\n";
        std::cin >> exit_char;
        if (exit_char == 'N' || exit_char == 'n')
        {
            break;
        }
    }

    return 0;
}
4

1 回答 1

1

@AlexanderZinovyev 感谢您的进一步解释,我可以通过将比例因子保持为 150% 并重新启动计算机来重现您的问题。

检查GetDeviceCaps API 文档后,我发现以下注释:

注意 GetDeviceCaps 报告显示驱动程序提供的信息。如果显示驱动程序拒绝报告任何信息,GetDeviceCaps 会根据固定计算来计算信息。如果显示驱动程序报告无效信息,GetDeviceCaps 将返回无效信息。此外,如果显示驱动程序拒绝报告信息,GetDeviceCaps 可能会计算不正确的信息,因为它假定固定 DPI (96 DPI) 或固定大小(取决于显示驱动程序提供和未提供的信息)。不幸的是,实现到 Windows 显示驱动程序模型 (WDDM)(在 Windows Vista 中引入)的显示驱动程序会导致 GDI 无法获取信息,因此 GetDeviceCaps 必须始终计算信息。

因此,GetDeviceCaps API 的返回值似乎无法反映设备的真实价值。

于 2019-01-25T09:40:49.437 回答