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

📄 bcgptoolbarmenubutton.cpp

📁 远程网络监视程序的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				CSize sizeExtra = CBCGPVisualManager::GetInstance ()->GetButtonExtraBorder ();
				if (sizeExtra != CSize (0, 0))
				{
					rectArrow.DeflateRect (sizeExtra.cx / 2 + 1, sizeExtra.cy / 2 + 1);
				}
			}

			if (m_bHorz)
			{
				rectArrow.left = rectArrow.right - 
					nScale * CMenuImages::Size ().cx - SEPARATOR_SIZE;
			}
			else
			{
				rectArrow.top = rectArrow.bottom - 
					nScale * CMenuImages::Size ().cy - SEPARATOR_SIZE;
			}

			m_bClickedOnMenu = rectArrow.PtInRect (ptMouse);
			if (!m_bClickedOnMenu)
			{
				return FALSE;
			}
		}
	}

	if (!m_bClickedOnMenu && m_nID > 0 && m_nID != (UINT) -1 && !m_bDrawDownArrow &&
		!m_bMenuOnly)
	{
		return FALSE;
	}

	CBCGPMenuBar* pMenuBar = DYNAMIC_DOWNCAST (CBCGPMenuBar, m_pWndParent);

	if (m_pPopupMenu != NULL)
	{
		//-----------------------------------------------------
		// Second click to the popup menu item closes the menu:
		//-----------------------------------------------------		
		ASSERT_VALID(m_pPopupMenu);

		m_pPopupMenu->m_bAutoDestroyParent = FALSE;
		m_pPopupMenu->DestroyWindow ();
		m_pPopupMenu = NULL;

		if (pMenuBar != NULL)
		{
			pMenuBar->SetHot (NULL);
		}
	}
	else
	{
		CBCGPPopupMenuBar* pParentMenu =
			DYNAMIC_DOWNCAST (CBCGPPopupMenuBar, m_pWndParent);

		if (bDelay && pParentMenu != NULL && !CBCGPToolBar::IsCustomizeMode ())
		{
			pParentMenu->StartPopupMenuTimer (this);
		}
		else
		{
			if (pMenuBar != NULL)
			{
				CBCGPToolbarMenuButton* pCurrPopupMenuButton = 
					pMenuBar->GetDroppedDownMenu();
				if (pCurrPopupMenuButton != NULL)
				{
					pCurrPopupMenuButton->OnCancelMode ();
				}
			}
			
			if (!OpenPopupMenu (pWnd))
			{
				return FALSE;
			}
		}

		if (pMenuBar != NULL)
		{
			pMenuBar->SetHot (this);
		}
	}

	if (m_pWndParent != NULL)
	{
		CRect rect = m_rect;

		const int nShadowSize = 
			CBCGPVisualManager::GetInstance ()->GetMenuShadowDepth ();

		rect.InflateRect (nShadowSize, nShadowSize);
		m_pWndParent->RedrawWindow (rect, NULL, RDW_FRAME | RDW_INVALIDATE);
	}

	return TRUE;
}
//****************************************************************************************
void CBCGPToolbarMenuButton::OnChangeParentWnd (CWnd* pWndParent)
{
	CBCGPToolbarButton::OnChangeParentWnd (pWndParent);

	if (pWndParent != NULL)
	{
		if (pWndParent->IsKindOf (RUNTIME_CLASS (CBCGPMenuBar)))
		{
            m_bDrawDownArrow = (m_nID != 0 && !m_listCommands.IsEmpty ()) ||
                ((CBCGPMenuBar *)pWndParent)->GetForceDownArrows();
			m_bText = TRUE;
			m_bImage = FALSE;
		}
		else
		{
			m_bDrawDownArrow = (m_nID == 0 || !m_listCommands.IsEmpty ());
		}

		if (pWndParent->IsKindOf (RUNTIME_CLASS (CBCGPPopupMenuBar)))
		{
			m_bMenuMode = TRUE;
			m_bText = TRUE;
			m_bImage = FALSE;
			m_bDrawDownArrow = (m_nID == 0 || !m_listCommands.IsEmpty ());
		}
		else
		{
			m_bMenuMode = FALSE;
		}
	}

	m_pWndParent = pWndParent;
}
//****************************************************************************************
void CBCGPToolbarMenuButton::CreateFromMenu (HMENU hMenu)
{
	while (!m_listCommands.IsEmpty ())
	{
		delete m_listCommands.RemoveHead ();
	}

	CMenu* pMenu = CMenu::FromHandle (hMenu);
	if (pMenu == NULL)
	{
		return;
	}

	UINT uiDefaultCmd = ::GetMenuDefaultItem (hMenu, FALSE, GMDI_USEDISABLED);

	int iCount = (int) pMenu->GetMenuItemCount ();
	for (int i = 0; i < iCount; i ++)
	{
		CBCGPToolbarMenuButton* pItem = new CBCGPToolbarMenuButton;
		ASSERT_VALID (pItem);

		pItem->m_nID = pMenu->GetMenuItemID (i);
		pMenu->GetMenuString (i, pItem->m_strText, MF_BYPOSITION);

		if (pItem->m_nID == -1)	// Sub-menu...
		{
			if (g_pTearOffMenuManager != NULL)
			{
				pItem->SetTearOff (g_pTearOffMenuManager->Parse (pItem->m_strText));
			}

			CMenu* pSubMenu = pMenu->GetSubMenu (i);
			pItem->CreateFromMenu (pSubMenu->GetSafeHmenu ());
		}
		else if (pItem->m_nID == uiDefaultCmd)
		{
			pItem->m_bDefault = TRUE;
		}

		UINT uiState = pMenu->GetMenuState (i, MF_BYPOSITION);

		if (uiState & MF_MENUBREAK)
		{
			pItem->m_nStyle |= TBBS_BREAK;
		}

		m_listCommands.AddTail (pItem);
	}
}
//****************************************************************************************
HMENU CBCGPToolbarMenuButton::CreateMenu () const
{
	if (m_listCommands.IsEmpty () && m_nID != (UINT) -1 && m_nID != 0 && !m_bMenuOnly)
	{
		return NULL;
	}

	CMenu menu;
	if (!menu.CreatePopupMenu ())
	{
		TRACE(_T("CBCGPToolbarMenuButton::CreateMenu (): Can't create popup menu!\n"));
		return NULL;
	}

	BOOL bRes = TRUE;
	DWORD dwLastError = 0;

	UINT uiDefaultCmd = (UINT) -1;

	int i = 0;
	for (POSITION pos = m_listCommands.GetHeadPosition (); pos != NULL; i ++)
	{
		CBCGPToolbarMenuButton* pItem = (CBCGPToolbarMenuButton*) m_listCommands.GetNext (pos);
		ASSERT (pItem != NULL);
		ASSERT_KINDOF (CBCGPToolbarButton, pItem);

		UINT uiStyle = MF_STRING;

		if (pItem->m_nStyle & TBBS_BREAK)
		{
			uiStyle |= MF_MENUBREAK;
		}

		if (pItem->IsTearOffMenu ())
		{
			uiStyle |= MF_MENUBARBREAK;
		}

		switch (pItem->m_nID)
		{
		case 0:	// Separator
			bRes = menu.AppendMenu (MF_SEPARATOR);
			if (!bRes)
			{
				dwLastError = GetLastError ();
			}
			break;

		case -1:			// Sub-menu
			{
				HMENU hSubMenu = pItem->CreateMenu ();
				ASSERT (hSubMenu != NULL);

				CString strText = pItem->m_strText;
				if (pItem->m_uiTearOffBarID != 0 && g_pTearOffMenuManager != NULL)
				{
					g_pTearOffMenuManager->Build (pItem->m_uiTearOffBarID, strText);
				}

				bRes = menu.AppendMenu (uiStyle | MF_POPUP, (UINT) hSubMenu, strText);
				if (!bRes)
				{
					dwLastError = GetLastError ();
				}

				//--------------------------------------------------------
				// This is incompatibility between Windows 95 and 
				// NT API! (IMHO). CMenu::AppendMenu with MF_POPUP flag 
				// COPIES sub-menu resource under the Win NT and 
				// MOVES sub-menu under Win 95/98 and 2000!
				//--------------------------------------------------------
				if (globalData.bIsWindowsNT4)
				{
					::DestroyMenu (hSubMenu);
				}
			}
			break;

		default:
			if (pItem->m_bDefault)
			{
				uiDefaultCmd = pItem->m_nID;
			}

			bRes = menu.AppendMenu (uiStyle, pItem->m_nID, pItem->m_strText);
			if (!bRes)
			{
				dwLastError = GetLastError ();
			}
		}

		if (!bRes)
		{
			TRACE(_T("CBCGPToolbarMenuButton::CreateMenu (): Can't add menu item: %d\n Last error = %x\n"), pItem->m_nID, dwLastError);
			return NULL;
		}
	}

	HMENU hMenu = menu.Detach ();
	if (uiDefaultCmd != (UINT)-1)
	{
		::SetMenuDefaultItem (hMenu, uiDefaultCmd, FALSE);
	}

	return hMenu;
}
//*****************************************************************************************
void CBCGPToolbarMenuButton::DrawMenuItem (CDC* pDC, const CRect& rect, CBCGPToolBarImages* pImages, 
					BOOL bCustomizeMode, BOOL bHighlight, BOOL bGrayDisabledButtons)
{
	ASSERT_VALID (pDC);
	ASSERT_VALID (this);

	BOOL bDisableImage = CMD_MGR.IsMenuItemWithoutImage (m_nID);

	CBCGPUserTool* pUserTool = NULL;
	if (g_pUserToolsManager != NULL && !m_bUserButton)
	{
		pUserTool = g_pUserToolsManager->FindTool (m_nID);
	}

	CSize sizeImage = CMenuImages::Size ();

	BOOL bDisabled = (bCustomizeMode && !IsEditable ()) ||
		(!bCustomizeMode && (m_nStyle & TBBS_DISABLED));

	if (m_pPopupMenu != NULL && !m_bToBeClosed)
	{
		bHighlight = TRUE;
	}

	CFont* pOldFont = NULL;

	CBCGPPopupMenuBar* pParentMenu =
		DYNAMIC_DOWNCAST (CBCGPPopupMenuBar, m_pWndParent);
	if (m_nID != 0 && m_nID != (UINT) -1 && !m_bMenuOnly && 
		pParentMenu != NULL && pParentMenu->GetDefaultMenuId () == m_nID)
	{
		pOldFont = (CFont*) pDC->SelectObject (&globalData.fontBold);
	}

	CRect rectImage;
	rectImage = rect;
	rectImage.left += CBCGPVisualManager::GetInstance ()->GetMenuImageMargin ();
	rectImage.right = rectImage.left + CBCGPToolBar::GetMenuImageSize ().cx + 
		CBCGPVisualManager::GetInstance ()->GetMenuImageMargin ();

	CRect rectFrameBtn = rectImage;
	rectFrameBtn.InflateRect (1, -1);

	BOOL bIsRarelyUsed = (CBCGPMenuBar::IsRecentlyUsedMenus () && 
		CBCGPToolBar::IsCommandRarelyUsed (m_nID));
	
	if (bIsRarelyUsed)
	{
		bIsRarelyUsed = FALSE;

		CBCGPPopupMenuBar* pParentMenuBar =
			DYNAMIC_DOWNCAST (CBCGPPopupMenuBar, m_pWndParent);

		if (pParentMenuBar != NULL)
		{
			CBCGPPopupMenu* pParentMenu = DYNAMIC_DOWNCAST (CBCGPPopupMenu, 
				pParentMenuBar->GetParent ());
			if (pParentMenu != NULL && pParentMenu->HideRarelyUsedCommands ())
			{
				bIsRarelyUsed = TRUE;
			}
		}
	}

	BOOL bLightImage = FALSE;
	BOOL bFadeImage = !bHighlight && CBCGPVisualManager::GetInstance ()->IsFadeInactiveImage ();

	if (bIsRarelyUsed)
	{
		bLightImage = TRUE;
		if (bHighlight && (m_nStyle & (TBBS_CHECKED | TBBS_INDETERMINATE)))
		{
			bLightImage = FALSE;
		}

		if (GetImage () < 0 && !(m_nStyle & (TBBS_CHECKED | TBBS_INDETERMINATE)))
		{
			bLightImage = FALSE;
		}
	}
	else if (m_nStyle & (TBBS_CHECKED | TBBS_INDETERMINATE))
	{
		bLightImage = !bHighlight;
	}

	//----------------
	// Draw the image:
	//----------------
	if (!IsDrawImage ())	// Try to find a matched image
	{
		BOOL bImageSave = m_bImage;
		BOOL bUserButton = m_bUserButton;
		BOOL bSuccess = TRUE;

		m_bImage = TRUE;	// Always try to draw image!
		m_bUserButton = TRUE;

		if (GetImage () < 0)
		{
			m_bUserButton = FALSE;

			if (GetImage () < 0)
			{
				bSuccess = FALSE;
			}
		}

		if (!bSuccess)
		{
			m_bImage = bImageSave;
			m_bUserButton = bUserButton;
		}
	}

	BOOL bImageIsReady = FALSE;

	CRgn rgnClip;
	rgnClip.CreateRectRgnIndirect (&rectImage);

	FillInterior (pDC, rectFrameBtn, bHighlight);

	if (!bDisableImage && IsDrawImage () && pImages != NULL)
	{
		BOOL bDrawImageShadow = 
			bHighlight && !bCustomizeMode &&
			CBCGPVisualManager::GetInstance ()->IsShadowHighlightedImage () &&
			((m_nStyle & TBBS_CHECKED) == 0) &&
			((m_nStyle & TBBS_DISABLED) == 0);

		pDC->SelectObject (&rgnClip);

		CPoint ptImageOffset (
			(rectImage.Width () - CBCGPToolBar::GetMenuImageSize ().cx) / 2, 
			(rectImage.Height () - CBCGPToolBar::GetMenuImageSize ().cy) / 2);

		if ((m_nStyle & TBBS_CHECKED) && !bCustomizeMode &&
			!CBCGPVisualManager::GetInstance ()->IsShadowHighlightedImage ())
		{
			ptImageOffset.x ++;
			ptImageOffset.y ++;

		}

		if ((m_nStyle & TBBS_PRESSED) || 
			!(m_nStyle & TBBS_DISABLED) ||
			!bGrayDisabledButtons ||
			bCustomizeMode)
		{
			if (pUserTool != NULL)
			{
				pUserTool->DrawToolIcon (pDC, 
					CRect (CPoint (rectImage.left + ptImageOffset.x, 
							rectImage.top + ptImageOffset.y),
					CBCGPToolBar::GetMenuImageSize ()));
			}
			else
			{
				CPoint pt = rectImage.TopLeft ();
				pt += ptImageOffset;

				if (bDrawImageShadow)
				{
					pImages->Draw (pDC, 
						pt.x,
						pt.y, 
						GetImage (), FALSE, FALSE, FALSE, TRUE);

					pt.Offset (-1, -1);
				}

				pImages->Draw (pDC, 
					pt.x, 
					pt.y, 
					GetImage ());
			}

			if (bCustomizeMode || m_nStyle & TBBS_PRESSED)
			{
				bImageIsReady = TRUE;
			}
		}

		if (!bImageIsReady)
		{
			if (pUserTool != NULL)
			{
				pUserTool->DrawToolIcon (pDC, 
					CRect (CPoint (rectImage.left + ptImageOffset.x, 
							rectImage.top + ptImageOffset.y),
					CBCGPToolBar::GetMenuImageSize ()));
			}
			else
			{
				if (bDrawImageShadow)
				{
					pImages->Draw (pDC, 
						rectImage.left + ptImageOffset.x,
						rectImage.top + ptImageOffset.y,
						GetImage (), FALSE, FALSE, FALSE, TRUE);

					rectImage.OffsetRect (-1, -1);
				}

				pImages->Draw (pDC, 
					rectImage.left + ptImageOffset.x, rectImage.top + ptImageOffset.y, 
					GetImage (), FALSE, bDisabled && bGrayDisabledButtons,
					FALSE, FALSE, bFadeImage);
			}

			bImageIsReady = TRUE;
		}
	}
	
	
	if (m_bAlwaysCallOwnerDraw || !bImageIsReady)
	{
		CFrameWnd* pParentFrame = m_pWndParent == NULL ?
			DYNAMIC_DOWNCAST (CFrameWnd, AfxGetMainWnd ()) :
			BCGCBProGetTopLevelFrame (m_pWndParent);

		//------------------------------------
		// Get chance to user draw menu image:
		//------------------------------------
		CBCGPMDIFrameWnd* pMainFrame = DYNAMIC_DOWNCAST (CBCGPMDIFrameWnd, pParentFrame);
		if (pMainFrame != NULL)
		{
			bImageIsReady = pMainFrame->OnDrawMenuImage (pDC, this, rectImage);
		}
		else	// Maybe, SDI frame...
		{
			CBCGPFrameWnd* pFrame = DYNAMIC_DOWNCAST (CBCGPFrameWnd, pParentFrame);
			if (pFrame != NULL)
			{
				bImageIsReady = pFrame->OnDrawMenuImage (pDC, this, rectImage);
			}
			else	// Maybe, OLE frame...
			{
				CBCGPOleIPFrameWnd* pOleFrame = 
					DYNAMIC_DOWNCAST (CBCGPOleIPFrameWnd, pParentFrame);
				if (pOleFrame != NULL)
				{
					bImageIsReady = pOleFrame->OnDrawMenuImage (pDC, this, rectImage);
				}
				else
				{
					CBCGPOleDocIPFrameWnd* pOleDocFrame = 
						DYNAMIC_DOWNCAST (CBCGPOleDocIPFrameWnd, pParentFrame);
					if (pOleDocFrame != NULL)
					{
						bImageIsReady = pOleDocFrame->OnDrawMenuImage (pDC, this, rectImage);
					}
				}

			}
		}
	}

	pDC->SelectClipRgn (NULL);

	rectImage.right ++;

	if (m_nStyle & TBBS_CHECKED && !bImageIsReady)
	{
		CRect rectCheck = rectImage;
		rectCheck.DeflateRect (0, 1);

		if (!bLightImage && bIsRarelyUsed)
		{
			pDC->FillRect (rectCheck, &globalData.brBtnFace);

⌨️ 快捷键说明

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