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

📄 bcgtoolbarmenubutton.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 3 页
字号:
					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)
	{
		return FALSE;
	}

	CBCGMenuBar* pMenuBar = DYNAMIC_DOWNCAST (CBCGMenuBar, 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
	{
		CBCGPopupMenuBar* pParentMenu =
			DYNAMIC_DOWNCAST (CBCGPopupMenuBar, m_pWndParent);

		if (bDelay && pParentMenu != NULL && !CBCGToolBar::IsCustomizeMode ())
		{
			pParentMenu->StartPopupMenuTimer (this);
		}
		else
		{
			//------------------------------------
			// Fixed by Sven Ritter (SpeedProject)
			//------------------------------------
			if (pMenuBar != NULL)
			{
				CBCGToolbarMenuButton* pCurrPopupMenuButton = 
					pMenuBar->GetDroppedDownMenu();
				if (pCurrPopupMenuButton != NULL)
				{
					pCurrPopupMenuButton->OnCancelMode ();
				}
			}
			// ***  End *************************
			
			if (!OpenPopupMenu (pWnd))
			{
				return FALSE;
			}
		}

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

	if (m_pWndParent != NULL)
	{
		m_pWndParent->InvalidateRect (m_rect);
	}

	return TRUE;
}
//****************************************************************************************
void CBCGToolbarMenuButton::OnChangeParentWnd (CWnd* pWndParent)
{
	if (pWndParent != NULL)
	{
		if (pWndParent->IsKindOf (RUNTIME_CLASS (CBCGMenuBar)))
		{
			m_bDrawDownArrow = FALSE;
			m_bText = TRUE;
			m_bImage = FALSE;
		}
		else
		{
			m_bDrawDownArrow = (m_nID == 0 || !m_listCommands.IsEmpty ());
		}

		if (pWndParent->IsKindOf (RUNTIME_CLASS (CBCGPopupMenuBar)))
		{
			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 CBCGToolbarMenuButton::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 ++)
	{
		CBCGToolbarMenuButton* pItem = new CBCGToolbarMenuButton;
		ASSERT_VALID (pItem);

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

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

		/// By Guy Hachlili - support for the menu with breaks:
		if (pMenu->GetMenuState (i, MF_BYPOSITION) & MF_MENUBREAK)
		{
			pItem->m_nStyle |= TBBS_BREAK;
		}
		/////////////////////////////

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

	CMenu menu;
	if (!menu.CreatePopupMenu ())
	{
		TRACE(_T("CBCGToolbarMenuButton::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 ++)
	{
		CBCGToolbarMenuButton* pItem = (CBCGToolbarMenuButton*) m_listCommands.GetNext (pos);
		ASSERT (pItem != NULL);
		ASSERT_KINDOF (CBCGToolbarButton, pItem);

		UINT uiStyle = MF_STRING;
		/// By Guy Hachlili - support for the menu with breaks:
		if (pItem->m_nStyle & TBBS_BREAK)
		{
			uiStyle |= MF_MENUBREAK;
		}
		///////////////////////////////////////////////////////

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

				bRes = menu.AppendMenu (uiStyle | MF_POPUP, 
										(UINT) hSubMenu, pItem->m_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("CBCGToolbarMenuButton::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 CBCGToolbarMenuButton::DrawMenuItem (CDC* pDC, const CRect& rect, CBCGToolBarImages* pImages, 
					BOOL bCustomizeMode, BOOL bHighlight, BOOL bGrayDisabledButtons)
{
	ASSERT_VALID (pDC);
	ASSERT_VALID (this);

	CSize sizeImage = CMenuImages::Size ();

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

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

	CFont* pOldFont = NULL;

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

	CRect rectImage;
	rectImage = rect;
	rectImage.left += IMAGE_MARGIN;
	rectImage.right = rectImage.left + CBCGToolBar::GetMenuImageSize ().cx + IMAGE_MARGIN;

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

	BOOL bIsRarelyUsed = (CBCGMenuBar::IsRecentlyUsedMenus () && 
		CBCGToolBar::IsCommandRarelyUsed (m_nID));
	
	if (bIsRarelyUsed)
	{
		bIsRarelyUsed = FALSE;

		CBCGPopupMenuBar* pParentMenuBar =
			DYNAMIC_DOWNCAST (CBCGPopupMenuBar, m_pWndParent);

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

	BOOL bLightImage = FALSE;

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

	//---------------------
	// Fill "checked" area:
	//---------------------
	if (bLightImage)
	{
		CBCGToolBarImages::FillDitheredRect (pDC, rectFrameBtn);
	}

	//----------------
	// 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);

	if (IsDrawImage () && pImages != NULL)
	{
		pDC->SelectObject (&rgnClip);

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

		BOOL bPressed = FALSE;
		if ((m_nStyle & TBBS_CHECKED) && !bCustomizeMode)
		{
			ptImageOffset.x ++;
			ptImageOffset.y ++;
			bPressed = TRUE;
		}

		if ((m_nStyle & TBBS_PRESSED) || !(m_nStyle & TBBS_DISABLED) ||
						bCustomizeMode)
		{
			pImages->Draw (pDC, 
				rectImage.left + ptImageOffset.x, rectImage.top + ptImageOffset.y, 
				GetImage ());
			if (bCustomizeMode || m_nStyle & TBBS_PRESSED)
			{
				bImageIsReady = TRUE;
			}
		}

		if (!bImageIsReady)
		{
			pImages->Draw (pDC, 
				rectImage.left + ptImageOffset.x, rectImage.top + ptImageOffset.y, 
				GetImage (), bLightImage, bDisabled && bGrayDisabledButtons);
			bImageIsReady = TRUE;
		}
	}
	else
	{
		CFrameWnd* pParentFrame = m_pWndParent == NULL ?
			DYNAMIC_DOWNCAST (CFrameWnd, AfxGetMainWnd ()) :
			m_pWndParent->GetTopLevelFrame ();

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

⌨️ 快捷键说明

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