Querying Windows display scaling
我想以编程方式查询

1 2 3 4 5 | // Get desktop dc desktopDc = GetDC(NULL); // Get native resolution horizontalDPI = GetDeviceCaps(desktopDc,LOGPIXELSX); verticalDPI = GetDeviceCaps(desktopDc,LOGPIXELSY); |
但是,此代码始终为水平和垂直

这个答案解决了它:
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; } |