⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menubar.cpp

📁 这是一个串口监视程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		sizeResult.cy -= rect.Height();
		sizeResult.cx -= rect.Width();
		CSize size = CControlBar::CalcFixedLayout((dwMode & LM_STRETCH), (dwMode & LM_HORZ));
		sizeResult.cx = max(sizeResult.cx, size.cx);
		sizeResult.cy = max(sizeResult.cy, size.cy);
	}
	
	if (dwMode & LM_HORZ)

		sizeResult.cy+=5;
	else
		sizeResult.cx+=3;

	return sizeResult;
}

//******************************************************************
CSize CMenuBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
	LTRACE(_T("CMenuBar::CalcFixedLayout\n"));
	ASSERT_VALID(this);
	ASSERT(::IsWindow(m_hWnd));

	DWORD dwMode = bStretch ? LM_STRETCH : 0;
	dwMode |= bHorz ? LM_HORZ : 0;

	return CalcLayout(dwMode);
}
//******************************************************************

CSize CMenuBar::CalcDynamicLayout(int nLength, DWORD dwMode)
{
	LTRACE(_T("CMenuBar::CalcDynamicLayout\n"));
	if ((nLength == -1) && !(dwMode & LM_MRUWIDTH) && !(dwMode & LM_COMMIT) &&
		((dwMode & LM_HORZDOCK) || (dwMode & LM_VERTDOCK)))
	{
		LTRACE(_T("    FixedLayout\n"));
		return CalcFixedLayout(dwMode & LM_STRETCH, dwMode & LM_HORZDOCK);
	}
	return CalcLayout(dwMode, nLength);
}

// set m_bWrapped by nWidth
int CMenuBar::WrapMenuBar(int nCount, int nWidth)
{
//	LTRACE(_T("CMenuBar::WrapMenuBar\n"));
	int nResult = 0;
	int x = 0;

	for (int i = 0; i < nCount; ++i) {
		CMenuItem* pItem = m_arrItem[i];
		if (i+1 == nCount)
			return ++nResult;
		
		if (x + pItem->GetHorizontalSize().cx> nWidth) {// itself is over
			if (pItem->GetStyle() & MISTYLE_WRAPPABLE) {
				pItem->ModifyState(0, MISTATE_WRAP);
				++nResult;
				x = 0;
			}
		}
		else if (x + pItem->GetHorizontalSize().cx + 
				 m_arrItem[i+1]->GetHorizontalSize().cx> nWidth) {
			if (pItem->GetStyle() & MISTYLE_WRAPPABLE) {
				pItem->ModifyState(0, MISTATE_WRAP);
				++nResult;
				x = 0;
			}
		}
		else {
			pItem->ModifyState(MISTATE_WRAP, 0);
			x += pItem->GetHorizontalSize().cx;
		}
	}

	return nResult + 1;
}
//******************************************************************
// calc only size, by using m_bWrapped
CSize CMenuBar::CalcSize(int nCount)
{
	ASSERT(nCount > 0);
	CPoint cur(0,0);
	CSize sizeResult(0,0);
	int nWrap = 0;
	for (int i = 0; i < nCount; ++i) {
		CMenuItem* pItem = m_arrItem[i];
		sizeResult.cx = max(cur.x + pItem->GetHorizontalSize().cx, sizeResult.cx);
		sizeResult.cy = max(cur.y + pItem->GetHorizontalSize().cy, sizeResult.cy);

		cur.x += pItem->GetHorizontalSize().cx;
		
		if (pItem->GetState() & MISTATE_WRAP) {
			//LTRACE(_T("    nIndex:%d is wrapped\n"), i);
			cur.x = 0;// reset x pos
			cur.y += pItem->GetHorizontalSize().cy;
			++nWrap;
		}
	}

	return sizeResult;
}
//******************************************************************
void CMenuBar::Layout()
{
	ASSERT(m_bDelayedButtonLayout);

	m_bDelayedButtonLayout = FALSE;

	BOOL bHorz = (m_dwStyle & CBRS_ORIENT_HORZ) != 0;
	if ((m_dwStyle & CBRS_FLOATING) && (m_dwStyle & CBRS_SIZE_DYNAMIC))
		((CMenuBar*)this)->CalcDynamicLayout(0, LM_HORZ | LM_MRUWIDTH | LM_COMMIT);
	else if (bHorz)
		((CMenuBar*)this)->CalcDynamicLayout(0, LM_HORZ | LM_HORZDOCK | LM_COMMIT);
	else
		((CMenuBar*)this)->CalcDynamicLayout(0, LM_VERTDOCK | LM_COMMIT);
}
//******************************************************************
void CMenuBar::SizeMenuBar(int nLength, BOOL bVert)
{
	//LTRACE("CMenuBar::SizeMenuBar\n");
	int nCount = GetItemCount(); ASSERT(nCount > 0);

	if (!bVert) {				// nLength is horizontal length

		if (IsFloating()) {		// half size wrapping

			CSize sizeMax, sizeMin, sizeMid;

			// Wrap MenuBar vertically
			WrapMenuBar(nCount, 0);
			sizeMin = CalcSize(nCount);

			// Wrap MenuBar horizontally
			WrapMenuBar(nCount, 32767);
			sizeMax = CalcSize(nCount);

			// we can never understand this algorithm :), see CToolBar implementation
			while (sizeMin.cx < sizeMax.cx) {
//				LTRACE("looping sizeMin.cx:%d < sizeMax.cx:%d\n", sizeMin.cx, sizeMax.cx);
				sizeMid.cx = (sizeMin.cx + sizeMax.cx) / 2;
				WrapMenuBar(nCount, sizeMid.cx);
				sizeMid = CalcSize(nCount);
				if (sizeMid.cx == sizeMax.cx) {	// if you forget, it loops forever!
					return;
				}
//				LTRACE("    sizeMid : %d %d\n", sizeMid.cx, sizeMid.cy);

				if (nLength >= sizeMax.cx) {
//					LTRACE("    nLength:%d >= sizeMax.cx:%d\n", nLength, sizeMax.cx);
					if (sizeMin == sizeMid) {
						WrapMenuBar(nCount, sizeMax.cx);
//						LTRACE("out SizeMenuBar\n");
						return;
					}
					sizeMin = sizeMid;
				}
				else if (nLength < sizeMax.cx) {
//					LTRACE("    nLength:%d < sizeMax.cx:%d\n", nLength, sizeMax.cx);
					sizeMax = sizeMid;
				}
				else {
//					LTRACE("out SizeMenuBar\n");
					return;
				}
			}
		}
		else {								// each one wrapping
			//LTRACE("    just each one wrapping\n");
			WrapMenuBar(nCount, nLength);
		}
	}
	else {						// nLength is vertical length
		CSize sizeMax, sizeMin, sizeMid;

		// Wrap MenuBar vertically
		WrapMenuBar(nCount, 0);
		sizeMin = CalcSize(nCount);

		// Wrap MenuBar horizontally
		WrapMenuBar(nCount, 32767);
		sizeMax = CalcSize(nCount);

		while (sizeMin.cx < sizeMax.cx) {
			sizeMid.cx = (sizeMin.cx + sizeMax.cx) / 2;
			WrapMenuBar(nCount, sizeMid.cx);
			sizeMid = CalcSize(nCount);
			if (sizeMid.cx == sizeMax.cx) {
				return;
			}
			if (nLength < sizeMid.cy) {
				if (sizeMin == sizeMid) {
					WrapMenuBar(nCount, sizeMax.cx);
					//LTRACE("out SizeMenuBar\n");
					return;
				}
				sizeMin = sizeMid;
			}
			else if (nLength > sizeMid.cy)
				sizeMax = sizeMid;
			else {
				//LTRACE("out SizeMenuBar\n");
				return;
			}
		}
	}

	//LTRACE("out SizeMenuBar\n");
}
//***********************************************************************************
void CMenuBar::DrawCombo()
{
	CRect rect;
	GetClientRect(rect);
	CalcSizeItem();
	CWnd* pFrame = GetTopLevelFrame();
	BOOL bMaximized = FALSE;	
	//la idea es verificar que la ventana es MDI
	if (pFrame->IsKindOf(RUNTIME_CLASS(CMDIFrameWnd)))
	{
		ASSERT_VALID(pFrame);
		CMDIFrameWnd* pMDIFrame = STATIC_DOWNCAST(CMDIFrameWnd, pFrame);
		HWND hWndMDIClient = pMDIFrame->m_hWndMDIClient;
		ASSERT(::IsWindow(hWndMDIClient));
		HWND hWndChild = (HWND)::SendMessage(hWndMDIClient,
			WM_MDIGETACTIVE, 0, (LPARAM)&bMaximized);
	}
	if ((m_dwStyle & CBRS_ORIENT_HORZ) && m_bCombo==TRUE && !(m_dwStyle & CBRS_FLOATING))
	{
		CRect reccombo;
		cmb->GetClientRect (&reccombo);
		if (rect.Width() > m_sizex+(bMaximized?30:0)+reccombo.Width() )
		{
			cmb->ShowWindow(SW_SHOW);				
			CRect rctemp=rect;
			rctemp.top=-1;
			rctemp.bottom-=2;
			int nDif;
			if (!bIsTabbed)
			   nDif=bMaximized?60:0;
			else
			   nDif=0;	
			rctemp.left=rctemp.right-reccombo.Width()-  nDif;
			rctemp.right=rctemp.left+reccombo.Width();
			cmb->MoveWindow(rctemp);	
		}
		else
		{
			cmb->ShowWindow(SW_HIDE);
		}
	}	

}

//******************************************************************
void CMenuBar::CalcSizeItem()
{
	m_sizex=0;
	for(int i = 0; i < m_arrItem.GetSize(); ++i) {
		CMenuItem* pItem = m_arrItem[i];
		m_sizex+=pItem->GetRect().Width();
	}

}

//******************************************************************
CSize CMenuBar::CalcVertDockSize(int nCount)
{
	ASSERT(nCount > 0);
	CSize sizeResult(0, 0);
	
	for (int i = 0; i < nCount; ++i) {
		CMenuItem* pItem = m_arrItem[i];
		sizeResult.cy += pItem->GetRect().Height();
	}

	sizeResult.cx = _cyMenuOnBar;
	return sizeResult;
}

//******************************************************************
void CMenuBar::CalcItemLayout(int nCount, BOOL bVert)
{
	ASSERT(nCount > 0);
	int x = 0; int y = 0;

	if (!bVert) {
		for (int i = 0; i < nCount; ++i) {
			CMenuItem* pItem = m_arrItem[i];
			CPoint ptItem(x, y);
			pItem->Layout(ptItem, TRUE);// layout by itself!
			if (pItem->GetState() & MISTATE_WRAP) {
				x = 0;// reset x to 0
				y += pItem->GetRect().Height();
			}
			else
				x += pItem->GetRect().Width();
		}		
	}
	else {
		for (int i = 0; i < nCount; ++i) {
			CMenuItem* pItem = m_arrItem[i];
			CPoint	ptItem(0, y);
			pItem->Layout(ptItem, FALSE);	// layout by itself
			y += pItem->GetRect().Height();
		}
	}
}
//******************************************************************
//////////////////////////////////////////////////////////////////////
// Added by Koay Kah Hoe. Thanx!
void CMenuBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
	if (m_bMDIApp)
		CheckActiveChildWndMaximized();
}
//******************************************************************
//////////////////////////////////////////////////////////////////////
// CMenuBar decoration

void CMenuBar::_DrawGripper(CWindowDC* dc,CRect* rcWin)
{


	if (m_dwStyle & CBRS_FLOATING) return ;
	if (m_dwStyle & CBRS_ORIENT_HORZ)
		{
			
			rcWin->top+=9;
			rcWin->left+=7;
			rcWin->right=rcWin->left+3;
			rcWin->bottom-=3;

			for (int i=0; i < rcWin->Height(); i+=2)
			{
				CRect rcWindow;
				CBrush cb;
				cb.CreateSolidBrush(::GetSysColor(COLOR_BTNSHADOW));
				rcWindow=rcWin;
				rcWindow.top=rcWin->top+i;
				rcWindow.bottom=rcWindow.top+1;
				dc->FillRect(rcWindow,&cb);
		
			}
			
		}
		else
		{
			rcWin->top+=2;
			rcWin->left+=2;
			rcWin->right-=2;
			rcWin->bottom=rcWin->top+3;
			for (int i=0; i < rcWin->Width(); i+=2)
			{
				CRect rcWindow;
				CBrush cb;
				cb.CreateSolidBrush(::GetSysColor(COLOR_BTNSHADOW));
				rcWindow=rcWin;
				rcWindow.left=rcWindow.left+i;
				rcWindow.right=rcWindow.left+1;
				dc->FillRect(rcWindow,&cb);
		
			}

		}


}

//******************************************************************
void CMenuBar::EraseNonClientEx()
{
	// get window DC that is clipped to the non-client area
	CWindowDC dc(this);
	CRect rectClient;
	GetClientRect(rectClient);
	CRect rectWindow;

	GetWindowRect(rectWindow);
	ScreenToClient(rectWindow);
	rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
	dc.ExcludeClipRect(rectClient);
	
	// draw borders in non-client area
	rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);

	dc.IntersectClipRect(rectWindow);
	SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);
	
	// draw gripper in non-client area
	_DrawGripper(&dc, &rectWindow);
}

#define CX_BORDER   1
#define CY_BORDER   1
//******************************************************************
void CMenuBar::DrawRaisedBorders(CDC* pDC, CRect& rect)
{
	ASSERT_VALID(this);
	ASSERT_VALID(pDC);

	DWORD dwStyle = m_dwStyle;
	if (!(dwStyle & CBRS_BORDER_ANY))
		return;

	// prepare for dark lines
	ASSERT(rect.top == 0 && rect.left == 0);
	CRect rect1, rect2;
	rect1 = rect;
	rect2 = rect;
	COLORREF clrBtnShadow = ::GetSysColor(COLOR_BTNSHADOW);//afxData.bWin4 ? afxData.clrBtnShadow : afxData.clrWindowFrame;
	COLORREF clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
	COLORREF clrBtnHilight = ::GetSysColor(COLOR_BTNHILIGHT);

	// draw dark line one pixel back/up
	if (dwStyle & CBRS_BORDER_3D)
	{
		rect1.right -= CX_BORDER;
		rect1.bottom -= CY_BORDER;
	}
	if (dwStyle & CBRS_BORDER_TOP)
		rect2.top += cyBorder2;
	if (dwStyle & CBRS_BORDER_BOTTOM)
		rect2.bottom -= cyBorder2;

	// draw left and top
	if (dwStyle & CBRS_BORDER_LEFT)
		pDC->FillSolidRect(0, rect2.top, CX_BORDER, rect2.Height(), clrBtnFace);
	if (dwStyle & CBRS_BORDER_TOP)
		pDC->FillSolidRect(0, 0, rect.right, CY_BORDER, clrBtnFace);

	// draw right and bottom
	if (dwStyle & CBRS_BORDER_RIGHT)
		pDC->FillSolidRect(rect1.right, rect2.top, -CX_BORDER, rect2.Height(), clrBtnShadow);
	if (dwStyle & CBRS_BORDER_BOTTOM)
		pDC->FillSolidRect(0, rect1.bottom, rect.right, -CY_BORDER, clrBtnShadow);

	if (dwStyle & CBRS_BORDER_3D)
	{
		// draw left and top
		if (dwStyle & CBRS_BORDER_LEFT)
			pDC->FillSolidRect(1, rect2.top, CX_BORDER, rect2.Height(), clrBtnHilight);
		if (dwStyle & CBRS_BORDER_TOP)
			pDC->FillSolidRect(0, 1, rect.right, CY_BORDER, clrBtnHilight);

		// draw right and bottom
		if (dwStyle & CBRS_BORDER_RIGHT)
			pDC->FillSolidRect(rect.right, rect2.top, -CX_BORDER, rect2.Height(), clrBtnFace);
		if (dwStyle & CBRS_BORDER_BOTTOM)
			pDC->FillSolidRect(0, rect.bottom, rect.right, -CY_BORDER, clrBtnFace);
	}

	if (dwStyle & CBRS_BORDER_LEFT)
		rect.left += cxBorder2;
	if (dwStyle & CBRS_BORDER_TOP)
		rect.top += cyBorder2;
	if (dwStyle & CBRS_BORDER_RIGHT)
		rect.right -= cxBorder2;
	if (dwStyle & CBRS_BORDER_BOTTOM)
		rect.bottom -= cyBorder2;		
}

//******************************************************************
int CMenuBar::GetNextOrPrevButton(int nIndex, BOOL bPrev)
{
	int nCount = GetItemCount();
	if (bPrev) {				// <-
		--nIndex;
		if (nIndex < 0)
			nIndex = nCount - 1;
	}
	else {						// ->
		++nIndex;
		if (nIndex >= nCount)
			nIndex = 0;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -