【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!搞定!

相关推荐
S1998_1997111609•X14 分钟前
MacOS/ˉsh(so.))os.apkair/AI
开发语言·网络·人工智能
SimpleLearingAI15 分钟前
C++虚函数详解
开发语言·c++
Dxy123931021633 分钟前
Python使用XPath定位元素:动态计算与函数调用
开发语言·python
小柯博客37 分钟前
STM32MP2安全启动技术深度解析
c语言·c++·stm32·嵌入式硬件·安全·开源·github
cpp_25011 小时前
P1832 A+B Problem(再升级)
数据结构·c++·算法·动态规划·题解·洛谷·背包dp
Evand J1 小时前
【MATLAB代码介绍】三种CT模型的IMM(交互式多模型)对目标高精度定位
开发语言·matlab·ct·imm·交互式多模型·多模型·转弯
AC赳赳老秦1 小时前
OpenClaw权限管理实操:团队共享Agent,设置操作权限,保障数据安全
服务器·开发语言·前端·javascript·excel·deepseek·openclaw
geovindu1 小时前
go: Proxy Pattern
开发语言·后端·设计模式·golang·代理模式
langsiming1 小时前
【无标题】
java·开发语言·数据库
꧁细听勿语情꧂1 小时前
合并两个有序表、判断链表的回文结构、相交链表、环的链表一和二
c语言·开发语言·数据结构·算法