【问题标题】:Querying Windows display scaling查询窗口显示缩放
【发布时间】:2019-07-21 13:30:02
【问题描述】:

我想以编程方式查询Windows 显示缩放设置: 在这种情况下,我希望它返回125,因为我将显示器配置为125% 缩放。根据this的文章,可以使用下面的Windows API C++代码:

// Get desktop dc
desktopDc = GetDC(NULL);
// Get native resolution
horizontalDPI = GetDeviceCaps(desktopDc,LOGPIXELSX);
verticalDPI = GetDeviceCaps(desktopDc,LOGPIXELSY);

但是,此代码始终返回 9696 用于水平和垂直 DPI 转换为 100% 缩放(根据提供的表格):

这个输出是错误的,因为我仍然得到与125% 缩放相同的结果。如何做呢?我在Java 中编程,所以我可以使用JNA 执行C++Windows API 解决方案是首选,但其他一切,如 .bat 脚本或 registry 查询也可以,只要它对于从 710 的所有 Windows 版本都是可靠的。

【问题讨论】:

  • 请注意,LOGPIXELSY 将始终返回 96 以保证兼容性,除非您将应用标记为 dpi-aware。

标签: c++ windows winapi


【解决方案1】:

This回答解决了:

#include "pch.h"
#include <iostream>
#include <windows.h>

int main()
{
    auto activeWindow = GetActiveWindow();
    HMONITOR monitor = MonitorFromWindow(activeWindow, MONITOR_DEFAULTTONEAREST);

    // Get the logical width and height of the monitor
    MONITORINFOEX monitorInfoEx;
    monitorInfoEx.cbSize = sizeof(monitorInfoEx);
    GetMonitorInfo(monitor, &monitorInfoEx);
    auto cxLogical = monitorInfoEx.rcMonitor.right - monitorInfoEx.rcMonitor.left;
    auto cyLogical = monitorInfoEx.rcMonitor.bottom - monitorInfoEx.rcMonitor.top;

    // Get the physical width and height of the monitor
    DEVMODE devMode;
    devMode.dmSize = sizeof(devMode);
    devMode.dmDriverExtra = 0;
    EnumDisplaySettings(monitorInfoEx.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
    auto cxPhysical = devMode.dmPelsWidth;
    auto cyPhysical = devMode.dmPelsHeight;

    // Calculate the scaling factor
    auto horizontalScale = ((double) cxPhysical / (double) cxLogical);
    auto verticalScale = ((double) cyPhysical / (double) cyLogical);

    std::cout << "Horizonzal scaling: " << horizontalScale << "\n";
    std::cout << "Vertical scaling: " << verticalScale;
}

【讨论】:

  • 有一些违反直觉的事情:当我首先在 main 中调用 SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); 时,它会报告所有监视器的因子 1.0。如果我不这样做,则这些因素与控制面板中显示的每台显示器的因素相匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
相关资源
最近更新 更多