本方法参考 https://www.cnblogs.com/zhaotianff/p/17296406.html实现
#include <windows.h> // 包含 Windows API 函数和数据结构
#include <iostream> // 用于 std::cout
qreal cx;
qreal scale;
float GetMonitorScalingRatio(HMONITOR monitor)
{
MONITORINFOEX info ={ };
info.cbSize = sizeof(info);
GetMonitorInfo(monitor, &info);
DEVMODE devmode = {};
devmode.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(info.szDevice, ENUM_CURRENT_SETTINGS, &devmode);
return static_cast<float>(devmode.dmPelsWidth) / (info.rcMonitor.right - info.rcMonitor.left);
}
void PrintMonitorInfo(HMONITOR monitor) {
MONITORINFOEX info = {};
info.cbSize = sizeof(info);
if (GetMonitorInfo(monitor, &info)) {
std::cout << "Monitor Name: " << info.szDevice << '\n';
std::cout << "Resolution: "
<< (info.rcMonitor.right - info.rcMonitor.left) << "x"
<< (info.rcMonitor.bottom - info.rcMonitor.top) << '\n';
// 获取原始分辨率
DEVMODE originalMode = {};
originalMode.dmSize = sizeof(DEVMODE);
if (EnumDisplaySettings(info.szDevice, ENUM_REGISTRY_SETTINGS, &originalMode)) {
std::cout << "Original Resolution: "
<< originalMode.dmPelsWidth << "x"
<< originalMode.dmPelsHeight << '\n';
} else {
std::cerr << "Failed to get original display settings for " << info.szDevice << ".\n";
}
} else {
std::cerr << "Failed to get monitor info.\n";
}
}
void getScreenInfo() {
::EnumDisplayMonitors(
nullptr,
nullptr,
[](HMONITOR hMonitor,
HDC Arg2,
LPRECT Arg3,
LPARAM Arg4)
{
PrintMonitorInfo(hMonitor);
float s = GetMonitorScalingRatio(hMonitor);
std::cout << s << std::endl;
return TRUE;
},
0);
}
运行结果
Monitor Name: 0x83f938
Resolution: 3072x1728
Original Resolution: 3840x2160
1.25
Monitor Name: 0x83f938
Resolution: 2560x1440
Original Resolution: 3840x2160
1.5