关于C ++:查询Windows显示比例

Querying Windows display scaling

我想以编程方式查询Windows显示比例设置:
><br />
在这种情况下,我希望它返回<wyn>125</wyn>,因为我将显示器配置为以<wyn>125%</wyn>缩放。 根据本文,可以使用以下<wyn>Windows API C++</wyn>代码:
</p>
<div class=

1
2
3
4
5
// Get desktop dc
desktopDc = GetDC(NULL);
// Get native resolution
horizontalDPI = GetDeviceCaps(desktopDc,LOGPIXELSX);
verticalDPI = GetDeviceCaps(desktopDc,LOGPIXELSY);

但是,此代码始终为水平和垂直DPI返回9696,这将转换为100%缩放比例(根据提供的表):
>
</p>
<p>
此输出是错误的,因为在<wyn>125%</wyn>缩放下我仍然会得到相同的结果。 如何做呢? 我正在<wyn>Java</wyn>中进行编程,因此可以使用<wyn>JNA</wyn>执行<wyn>C++</wyn>。 最好使用<wyn>Windows API</wyn>解决方案,但只要它对于从<wyn>7</wyn>到<wyn>10</wyn>的所有<wyn>Windows</wyn>版本均可靠,则诸如<wyn>.bat</wyn>脚本或<wyn>registry</wyn>查询之类的其他所有内容也都可以。
</p>
<div class=


这个答案解决了它:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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 <<"\
"
;
    std::cout <<"Vertical scaling:" << verticalScale;
}