查询Windows显示缩放 [英] Querying Windows display scaling
问题描述
我想以编程方式查询 Windows
显示缩放设置:在这种情况下,我希望它返回 125
,因为我将显示配置为 125%
缩放.根据
这个输出是错误的,因为使用 125%
缩放我仍然得到相同的结果.怎么做到呢?我正在用 Java
编程,所以我可以使用 JNA
.Windows API
解决方案是首选,但其他所有内容,如 .bat
脚本或 registry
查询也可以,只要它对所有 都是可靠的Windows
版本从 7
到 10
.
这个答案解决了:
#include "pch.h"#include #include int main(){自动 activeWindow = GetActiveWindow();HMONITOR 监视器 = MonitorFromWindow(activeWindow, MONITOR_DEFAULTONEAREST);//获取监视器的逻辑宽度和高度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;//获取显示器的物理宽度和高度开发模式 devMode;devMode.dmSize = sizeof(devMode);devMode.dmDriverExtra = 0;EnumDisplaySettings(monitorInfoEx.szDevice, ENUM_CURRENT_SETTINGS, &devMode);auto cxPhysical = devMode.dmPelsWidth;auto cyPhysical = devMode.dmPelsHeight;//计算缩放因子自动水平刻度 = ((双) cxPhysical/(双) cxLogical);auto verticalScale = ((double) cyPhysical/(double) cyLogical);std::cout <<"水平缩放:" <<水平比例<<"\n";std::cout <<"垂直缩放:" <<垂直刻度;}
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 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):
This output is wrong since I still get the same result with 125%
scaling. How can it be done? I'm programming in Java
so I can execute C++
using JNA
. Windows API
solutions are preferred but everything else like a .bat
script or registry
query is also fine as long as it's reliable for all Windows
versions from 7
to 10
.
This answer solved it:
#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;
}
这篇关于查询Windows显示缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!