查询Windows显示比例

时间:2019-02-27 18:16:54

标签: c++ windows winapi

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

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

但是,此代码始终为水平96和垂直96返回DPI100%,这将转换为125%缩放比例(根据提供的表格): this

此输出是错误的,因为通过Java缩放仍然可以获得相同的结果。怎么做到呢?我正在C++中编程,因此可以使用执行Windows API。首选.bat解决方案,但只要registryWindows至{ {1}}。

1 个答案:

答案 0 :(得分:0)

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;
}