c++获取屏幕分辨率DPI以及屏幕放缩倍数

c++获取屏幕分辨率DPI以及屏幕放缩倍数

参考示例

	// 获取分辨率不准
	nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
	nScreenHeight = GetSystemMetrics(SM_CYSCREEN);

	// 获取创口句柄
	HWND hWnd = GetDesktopWindow();
	HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
	// 获取监视器逻辑尺寸
	MONITORINFOEX miex;
	miex.cbSize = sizeof(miex);
	GetMonitorInfo(hMonitor, &miex);
	int cxLogical = (miex.rcMonitor.right - miex.rcMonitor.left);
	int cyLogical = (miex.rcMonitor.bottom - miex.rcMonitor.top);

	// 获取监视器物理宽度与高度
	DEVMODE dm;
	dm.dmSize = sizeof(dm);
	dm.dmDriverExtra = 0;
	EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
	int cxPhysical = dm.dmPelsWidth;
	int cyPhysical = dm.dmPelsHeight;

	// 缩放比例计算  实际上使用任何一个即可
	double horzScale = ((double)cxPhysical / (double)cxLogical);
	double vertScale = ((double)cyPhysical / (double)cyLogical);
	//assert(horzScale == vertScale); // 宽或高这个缩放值应该是相等的
	nScreenWidthCoef = (double(nScreenWidth) / 1680.0)* horzScale;
	nScreenHeightCoef = (double(nScreenHeight) / 1050.0)* vertScale;
	ImGuiDebugVec.push_back(u8"当前屏幕 XDpi = " + std::to_string(horzScale));
	ImGuiDebugVec.push_back(u8"当前屏幕 YDpi = " + std::to_string(vertScale));

	ImGuiDebugVec.push_back(u8"当前屏幕逻辑分辨率::" + std::to_string(nScreenWidth) + "  ->  " + std::to_string(nScreenWidthCoef) + "  适配倍率:: " +
		std::to_string(nScreenHeight) + "  ->  " + std::to_string(nScreenHeightCoef));
	srand((int)time(NULL));

版权声明:本文为qq_27100753原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。