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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With