多显示器操作

 

1. 获得窗口所在的显示器句柄

  The MonitorFromWindow function retrieves a handle to the display monitor that has the largest area of intersection with the bounding rectangle of a specified window.

  HMONITOR MonitorFromWindow(
  HWND  hwnd,
  DWORD dwFlags
);

 其中,dwFlags,Determines the function's return value if the window does not intersect any display monitor.

ValueMeaning

MONITOR_DEFAULTTONEAREST

Returns a handle to the display monitor that is nearest to the window.

MONITOR_DEFAULTTONULL

Returns NULL.

MONITOR_DEFAULTTOPRIMARY

Returns a handle to the primary display monitor.

 示例

  HMONITOR  hMonitor=::MonitorFromWindow(m_hParent, MONITOR_DEFAULTTOPRIMARY);

 

2 获得显示器信息

   GetMonitorInfo

   示例:

	MONITORINFO oMonitor = {}; 
	oMonitor.cbSize = sizeof(oMonitor);


    HMONITOR  hMonitor=::MonitorFromWindow(m_hParent, MONITOR_DEFAULTTOPRIMARY);
    ::GetMonitorInfo(hMonitor, &oMonitor);

其中,rcMonitor包括底部任务栏,rcWork 是工作区,不包括底部任务栏

 

由此可获知显示器分辨率等信息

ui::CSize szInit = { monitor_rect.right - monitor_rect.left, monitor_rect.bottom - monitor_rect.top };

    

3 获得各个显示器

EnumDisplayMonitors(
    __in_opt HDC hdc,
    __in_opt LPCRECT lprcClip,
    __in MONITORENUMPROC lpfnEnum,
    __in LPARAM dwData);

 

示例1:

 

	std::vector<HMONITOR> m_monitorVec;
	BOOL CALLBACK MyMonitorEnumProc(_In_ HMONITOR hMonitor, _In_ HDC hdcMonitor, _In_ LPRECT lprcMonitor, _In_ LPARAM dwData)
	{
		if (hMonitor == nullptr)
		{
			assert(false);
			return TRUE; //不要return FALSE,否则会结束枚举显示器
		}


		m_monitorVec.push_back(hMonitor);

		return TRUE;
	}

 

	//多屏操作
	void EnumMonitors(){
		
		m_monitorVec.clear();
		::EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, 0);
	}

 

 

示例2:

static BOOL CALLBACK AddMonitorsCallBack(HMONITOR hMonitor,HDC hdcMonitor,LPRECT lprcMonitor,LPARAM dwData);


void CMonitorManager::UpdateMonitors()
{
   FreeMonitors();
   ::EnumDisplayMonitors( NULL, NULL, AddMonitorsCallBack, (LPARAM)this);
}
BOOL CALLBACK CMonitorManager::AddMonitorsCallBack(HMONITOR hMonitor,HDC hdcMonitor,LPRECT lprcMonitor,LPARAM dwData)
{
   CMonitorManager* pMonitorManger = (CMonitorManager*)dwData;
   int nIndex = pMonitorManger->m_vec_monitor.size();
   CMonitor* pMonitor = new CMonitor(hMonitor, ++nIndex);
   pMonitorManger->m_vec_monitor.push_back(pMonitor);
   return TRUE;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清水迎朝阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值