多显示器操作

版权声明:www.gudianxiaoshuo.com (古典小说网) 今日头条号: 古典古韵古典小说、讨厌编程 https://blog.csdn.net/shuilan0066/article/details/87286926

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.

Value Meaning

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;
}

猜你喜欢

转载自blog.csdn.net/shuilan0066/article/details/87286926