MFC中主副屏窗口全屏最大化的几种方法

MFC中主副屏窗口全屏最大化的几种方法

1、根据鼠标位置判断全屏位置

枚举所有窗口

CArray<MONITORINFO, MONITORINFO&> g_arrMonitorInfo;

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
	static BOOL bFirst = TRUE;
	MONITORINFO monitorinfo;
	monitorinfo.cbSize = sizeof(MONITORINFO);

	//获得显示器信息,将信息保存到monitorinfo中
	GetMonitorInfo(hMonitor, &monitorinfo);

	//若检测到主屏
	if (monitorinfo.dwFlags == MONITORINFOF_PRIMARY)
	{
		if (bFirst) //第一次检测到主屏
		{
			bFirst = FALSE;
		}
		else      //第二次检测到主屏,所有的监视器都已经检测了一遍,停止检测
		{
			bFirst = TRUE;
			return FALSE;
		}
	}

	g_arrMonitorInfo.Add(monitorinfo);
	return TRUE;
}

重写窗口最大化函数

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
	if (nType == SIZE_MAXIMIZED && !CUserOptionSettingsCache::Instance()->GetLoginBasicFullScreen())
	{
		g_arrMonitorInfo.RemoveAll();
		EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, NULL);

		//获取当前鼠标位置,判断将窗口全屏到那个屏幕
		CPoint ptScreen;
		GetCursorPos(&ptScreen);
		for (int i = 0; i < g_arrMonitorInfo.GetSize(); i++)
		{
			CRect rcScreen = g_arrMonitorInfo[i].rcMonitor;
			if (rcScreen.PtInRect(ptScreen))
			{
				CRect rcWorkArea = g_arrMonitorInfo[i].rcWork;
				CRect rc;
				rc.SubtractRect(rcScreen, rcWorkArea);
				rcWorkArea.InflateRect(
					(rc.Height() > rc.Width() && rc.left == rcScreen.left) ? 0 : 1,
					(rc.Width() > rc.Height() && rc.top == rcScreen.top) ? 0 : 1,
					(rc.Height() > rc.Width() && rc.right == rcScreen.right) ? 0 : 1,
					(rc.Width() > rc.Height() && rc.bottom == rcScreen.bottom) ? 0 : 1);
				MoveWindow(&rcWorkArea);
				break;
			}
		}
	CBCGPFrameWnd::OnSize(nType, cx, cy);
}

2、利用窗口在屏幕位置进行识别

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
	if (nType == SIZE_MAXIMIZED && !CUserOptionSettingsCache::Instance()->GetLoginBasicFullScreen())
	{
		//通过程序在那个屏幕中占比多判断全屏那个屏幕
		MONITORINFO mi;
		mi.cbSize = sizeof(MONITORINFO);
		if (GetMonitorInfo(MonitorFromWindow(this->GetSafeHwnd(), MONITOR_DEFAULTTONEAREST), &mi))
		{
			CRect rcScreen = mi.rcMonitor;
			CRect rcWorkArea = mi.rcWork;
			CRect rc;
			rc.SubtractRect(rcScreen, rcWorkArea);
			rcWorkArea.InflateRect(
				(rc.Height()>rc.Width() && rc.left == rcScreen.left) ? 0 : 1,
				(rc.Width()>rc.Height() && rc.top == rcScreen.top) ? 0 : 1,
				(rc.Height()>rc.Width() && rc.right == rcScreen.right) ? 0 : 1,
				(rc.Width()>rc.Height() && rc.bottom == rcScreen.bottom) ? 0 : 1);
			MoveWindow(&rcWorkArea);
		}
	}
	
	CBCGPFrameWnd::OnSize(nType, cx, cy);
}

你可能感兴趣的:(mfc,c++)