【C++】获取指定点所在屏幕的尺寸

问题

多个显示器时,获取指定点所在的显示器的尺寸。

分析

解决

cpp 复制代码
#include "WinUser.h"

void GetRectByPoint(CPoint point, CRect& rectRes)
{
	//获取显示屏幕个数
	int nCount = GetSystemMetrics(SM_CMONITORS);
	if (nCount < 0)
		return;
	
	DISPLAY_DEVICE device;
	SecureZeroMemory(&device, sizeof(device));
	device.cb = sizeof(device);
	DEVMODE devMode;
	SecureZeroMemory(&devMode, sizeof(devMode));
	devMode.dmSize = sizeof(devMode);
	
	// 获取所有显示屏幕的位置
	std::vector<CRect> vScreenRet;
	for (int nIndex = 0; nIndex < nCount; ++nIndex)
	{
		if (!EnumDisplayDevices(NULL, nIndex, &device, 0)
			|| !EnumDisplaySettings(device.DeviceName, ENUM_CURRENT_SETTINGS, &devMode))
			continue;
	
		CRect rc(devMode.dmPosition.x, devMode.dmPosition.y, devMode.dmPosition.x + devMode.dmPelsWidth, devMode.dmPosition.y + devMode.dmPelsHeight);
		vScreenRet.push_back(rc);
	}
	
	// 匹配指定点所在显示器
	for (const CRect& rc : vScreenRet)
	{
		if (!::PtInRect(rc, point))
			continue;
		resRt = rc;
		break;
	}
}


int main()
{
	// 给定点坐标
	CPoint point;
	
	// 获取显示器尺寸
	CRect rect;
	GetRectByPoint(point, rect);
	// 输出获取到的数据
	cout<<"rect width: " << rect.right-rect.left << endl; 
	cout<<"rect height: " << rect.bottom-rect.top << endl; 
	return 0;
}

OK!搞定!

相关推荐
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
埃博拉酱3 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21883 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
加号33 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20213 天前
信号量和信号
linux·c++