I want to query the Windows
display scaling setting programmatically:
In this case, I want it to return 125
since I configured my display to be at 125%
scaling. According to this https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings#scale article, the following Windows API C++
code can be used:
// Get desktop dc
desktopDc = GetDC(NULL);
// Get native resolution
horizontalDPI = GetDeviceCaps(desktopDc,LOGPIXELSX);
verticalDPI = GetDeviceCaps(desktopDc,LOGPIXELSY);
However, this code always return 96
and 96
for the horizontal as well as vertical DPI
which translates to 100%
scaling (according to the provided table):
这个输出是错误的,因为我仍然得到相同的结果125%
缩放。如何做呢?我正在编程Java
所以我可以执行C++
using JNA https://github.com/java-native-access/jna. Windows API
解决方案是首选,但其他一切都像.bat
脚本或registry
查询也可以,只要它对所有人都是可靠的Windows
版本来自7
to 10
.
This https://stackoverflow.com/a/36864741/3764804答案解决了它:
#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)