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

📄 bcgpopupmenu.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				//-----------------------
				// Scroll down is needed!
				//-----------------------
				iOffsetDelta = (pItem->Rect ().bottom - rectBar.bottom) / 
					pMenuBar->GetRowHeight () + 1;
			}

			if (iOffsetDelta != 0)
			{
				int iTotalRows = m_FinalSize.cy / pMenuBar->GetRowHeight () - 2;

				iOffset += iOffsetDelta;
				iOffset = min (max (0, iOffset), 
					pMenuBar->m_Buttons.GetCount () - iTotalRows - 1);

				pMenuBar->SetOffset (iOffset);
				pMenuBar->AdjustLayout ();

				InvalidateRect (m_rectScrollDn);
				InvalidateRect (m_rectScrollUp);
			}
		}
	}
}
//****************************************************************************************
CBCGPopupMenu* CBCGPopupMenu::GetParentPopupMenu () const
{
	if (m_pParentBtn == NULL)
	{
		return NULL;
	}

	CBCGPopupMenuBar* pParentBar = 
		DYNAMIC_DOWNCAST (CBCGPopupMenuBar, m_pParentBtn->m_pWndParent);
	if (pParentBar != NULL)
	{
		CBCGPopupMenu* pParentMenu =
			DYNAMIC_DOWNCAST (CBCGPopupMenu, pParentBar->GetParentFrame ());
		ASSERT_VALID (pParentMenu);

		return pParentMenu;
	}
	else
	{
		return NULL;
	}
}
//****************************************************************************************
CBCGToolBar* CBCGPopupMenu::GetParentToolBar () const
{
	if (m_pParentBtn == NULL)
	{
		return NULL;
	}

	CBCGToolBar* pParentBar = 
		DYNAMIC_DOWNCAST (CBCGToolBar, m_pParentBtn->m_pWndParent);
	return pParentBar;
}
//****************************************************************************************
CBCGToolbarMenuButton* CBCGPopupMenu::GetSelItem ()
{
	CBCGPopupMenuBar* pMenuBar = GetMenuBar ();
	ASSERT_VALID (pMenuBar);

	return DYNAMIC_DOWNCAST (CBCGToolbarMenuButton,
							pMenuBar->GetHighlightedButton ());
}
//****************************************************************************************
void CBCGPopupMenu::CloseMenu (BOOL bSetFocusToBar)
{
	m_bTobeDstroyed = TRUE;

	SaveState ();

	CBCGPopupMenu* pParentMenu = GetParentPopupMenu ();
	CBCGToolBar* pParentToolBar = GetParentToolBar ();

	CFrameWnd* pWndMain = GetTopLevelFrame ();
	if (pParentMenu != NULL)
	{
		m_bAutoDestroyParent = FALSE;
		ActivatePopupMenu (pWndMain, pParentMenu);
	}
	else if (pParentToolBar != NULL)
	{
		ActivatePopupMenu (pWndMain, NULL);

		if (bSetFocusToBar)
		{
			pParentToolBar->SetFocus ();
		}
	}
	else
	{
		ActivatePopupMenu (pWndMain, NULL);
	}

	SendMessage (WM_CLOSE);
}
//****************************************************************************************
int CBCGPopupMenu::InsertItem (const CBCGToolbarMenuButton& button, int iInsertAt)
{
	CBCGPopupMenuBar* pMenuBar = GetMenuBar ();
	ASSERT_VALID (pMenuBar);

	return pMenuBar->InsertButton (button, iInsertAt);
}
//****************************************************************************************
int CBCGPopupMenu::InsertSeparator (int iInsertAt)
{
	CBCGPopupMenuBar* pMenuBar = GetMenuBar ();
	ASSERT_VALID (pMenuBar);

	return pMenuBar->InsertSeparator (iInsertAt);
}
//****************************************************************************************
int CBCGPopupMenu::GetMenuItemCount () const
{
	return m_wndMenuBar.m_Buttons.GetCount ();
}
//****************************************************************************************
CBCGToolbarMenuButton* CBCGPopupMenu::GetMenuItem (int iIndex) const
{
	return (CBCGToolbarMenuButton*) m_wndMenuBar.GetButton (iIndex);
}
//****************************************************************************************
BOOL CBCGPopupMenu::RemoveItem (int iIndex)
{
	CBCGPopupMenuBar* pMenuBar = GetMenuBar ();
	ASSERT_VALID (pMenuBar);

	return pMenuBar->RemoveButton (iIndex);
}
//****************************************************************************************
void CBCGPopupMenu::RemoveAllItems ()
{
	CBCGPopupMenuBar* pMenuBar = GetMenuBar ();
	ASSERT_VALID (pMenuBar);

	pMenuBar->RemoveAllButtons ();
}
//****************************************************************************************
BOOL CBCGPopupMenu::OnEraseBkgnd(CDC* /*pDC*/) 
{
	return TRUE;
}
//****************************************************************************************
BOOL CBCGPopupMenu::ActivatePopupMenu (CFrameWnd* pTopFrame, CBCGPopupMenu* pPopupMenu)
{
	ASSERT_VALID (pTopFrame);

	BOOL bRes = TRUE;

	CBCGMDIFrameWnd* pMainFrame = DYNAMIC_DOWNCAST (CBCGMDIFrameWnd, pTopFrame);
	if (pMainFrame != NULL)
	{
		bRes = pMainFrame->ShowPopupMenu (pPopupMenu);
	}
	else	// Maybe, SDI frame...
	{
		CBCGFrameWnd* pFrame = DYNAMIC_DOWNCAST (CBCGFrameWnd, pTopFrame);
		if (pFrame != NULL)
		{
			bRes = pFrame->ShowPopupMenu (pPopupMenu);
		}
		else	// Maybe, OLE frame
		{
			CBCGOleIPFrameWnd* pOleFrame = 
				DYNAMIC_DOWNCAST (CBCGOleIPFrameWnd, pTopFrame);
			if (pOleFrame != NULL)
			{
				bRes = pOleFrame->ShowPopupMenu (pPopupMenu);
			}
		}
	}

	if (!bRes)
	{
		if (pPopupMenu != NULL && !pPopupMenu->m_bTobeDstroyed)
		{
			pPopupMenu->CloseMenu ();
		}

		return FALSE;
	}

	if (pPopupMenu != NULL)
	{
		CBCGPopupMenuBar* pMenuBar = pPopupMenu->GetMenuBar ();
		ASSERT_VALID (pMenuBar);

		CBCGPopupMenu* pParentMenu = DYNAMIC_DOWNCAST (CBCGPopupMenu, pMenuBar->GetParent ());
		if (pParentMenu != NULL && pParentMenu->GetParentButton () != NULL &&
			!pMenuBar->m_bAreAllCommandsShown)
		{
			pMenuBar->InsertButton (CBCGShowAllButton ());
		}
	}

	return TRUE;
}
//************************************************************************************
void CBCGPopupMenu::OnActivateApp(BOOL bActive, HTASK /*hTask*/) 
{
	if (!bActive && !CBCGToolBar::IsCustomizeMode ())
	{
		SendMessage (WM_CLOSE);
	}
}
//*************************************************************************************
void CBCGPopupMenu::OnTimer(UINT nIDEvent) 
{
	CBCGPopupMenuBar* pMenuBar = GetMenuBar ();
	ASSERT_VALID (pMenuBar);

	switch (nIDEvent)
	{
	case iAnimTimerId:
		if (!m_bAnimationIsDone)
		{
			m_AnimSize.cy += pMenuBar->GetRowHeight ();
			if (m_AnimationType == UNFOLD)
			{
				m_AnimSize.cx += pMenuBar->GetColumnWidth ();
			}

			if (m_AnimSize.cy + m_iShadowSize >= m_FinalSize.cy ||
				(m_AnimationType == UNFOLD && m_AnimSize.cx + m_iShadowSize >= m_FinalSize.cx))
			{
				m_bAnimationIsDone = TRUE;

				m_AnimSize.cx = m_FinalSize.cx + m_iShadowSize;
				m_AnimSize.cy = m_FinalSize.cy + m_iShadowSize;

				m_ptLocationAnim = m_ptLocation;

				pMenuBar->SetOffset (0);
				KillTimer (iAnimTimerId);
			}
			else
			{
				if (m_bIsAnimDown)
				{
					pMenuBar->SetOffset (pMenuBar->GetOffset () - 1);
				}

				if (!m_bIsAnimRight && m_AnimationType == UNFOLD)
				{
					m_ptLocationAnim.x -= pMenuBar->GetColumnWidth ();
				}

				if (!m_bIsAnimDown)
				{
					m_ptLocationAnim.y -= pMenuBar->GetRowHeight ();
				}
			}

			SetWindowPos (NULL, m_ptLocationAnim.x, m_ptLocationAnim.y, 
				m_AnimSize.cx, m_AnimSize.cy,
				SWP_NOZORDER | SWP_NOACTIVATE);

			//----------------
			// Redraw borders:
			//----------------
			CRect rectClient;
			GetClientRect (rectClient);

			InvalidateRect (CRect (	CPoint (rectClient.left, rectClient.top),
									CSize (rectClient.Width (), iBorderSize)));
			InvalidateRect (CRect (	CPoint (rectClient.left, rectClient.top),
									CSize (iBorderSize, rectClient.Height ())));
			InvalidateRect (CRect (	CPoint (rectClient.right - iBorderSize, rectClient.top),
									CSize (iBorderSize, rectClient.Height ())));

			//-----------------------
			// Redraw scroll buttons:
			//-----------------------
			InvalidateRect (m_rectScrollUp);
			InvalidateRect (m_rectScrollDn);
		}
		break;

	case iScrollTimerId:
		{
			CPoint point;
			::GetCursorPos (&point);
			ScreenToClient (&point);

			int iOffset = pMenuBar->GetOffset ();

			if (m_rectScrollUp.PtInRect (point) && m_iScrollMode < 0 &&
				IsScrollUpAvailable ())	// Scroll Up
			{
				pMenuBar->SetOffset (iOffset - 1);
				pMenuBar->AdjustLayout ();
			}
			else if (m_rectScrollDn.PtInRect (point) && m_iScrollMode > 0 &&
				IsScrollDnAvailable ())	// Scroll Down
			{
				pMenuBar->SetOffset (iOffset + 1);
				pMenuBar->AdjustLayout ();
			}
			else
			{
				KillTimer (iScrollTimerId);
				m_iScrollMode = 0;
				InvalidateRect (m_rectScrollDn);
				InvalidateRect (m_rectScrollUp);
			}
		}
		break;
	}

	CMiniFrameWnd::OnTimer(nIDEvent);
}
//****************************************************************************************
void CBCGPopupMenu::OnMouseMove(UINT nFlags, CPoint point) 
{
	CMiniFrameWnd::OnMouseMove(nFlags, point);

	if (!m_bScrollable || m_iScrollMode != 0)
	{
		return;
	}
	
	if (m_rectScrollUp.PtInRect (point) && IsScrollUpAvailable ())
	{
		m_iScrollMode = -1;
		InvalidateRect (m_rectScrollUp);
	}
	else if (m_rectScrollDn.PtInRect (point) && IsScrollDnAvailable ())
	{
		m_iScrollMode = 1;
		InvalidateRect (m_rectScrollDn);
	}
	else
	{
		m_iScrollMode = 0;
	}

	if (m_iScrollMode != 0)
	{
		SetTimer (iScrollTimerId, iScrollTimerDuration, NULL);
	}
}
//****************************************************************************************
BOOL CBCGPopupMenu::IsScrollUpAvailable ()
{
	CBCGPopupMenuBar* pMenuBar = GetMenuBar ();
	ASSERT_VALID (pMenuBar);

	return pMenuBar->GetOffset () > 0;
}
//****************************************************************************************
BOOL CBCGPopupMenu::IsScrollDnAvailable ()
{
	CBCGPopupMenuBar* pMenuBar = GetMenuBar ();
	ASSERT_VALID (pMenuBar);

	if (pMenuBar->GetRowHeight () == 0)
	{
		return FALSE;
	}

	int iTotalRows = m_FinalSize.cy / pMenuBar->GetRowHeight () - 1;
	return (pMenuBar->GetOffset () < 
		pMenuBar->m_Buttons.GetCount () - iTotalRows);
}
//****************************************************************************************
void CBCGPopupMenu::CollapseSubmenus ()
{
	for (POSITION pos = m_wndMenuBar.m_Buttons.GetHeadPosition (); pos != NULL;)
	{
		CBCGToolbarButton* pButton = 
			(CBCGToolbarButton*) m_wndMenuBar.m_Buttons.GetNext (pos);
		ASSERT (pButton != NULL);

		pButton->OnCancelMode ();
	}
}
//****************************************************************************************
void CBCGPopupMenu::DrawImage (CDC* pDC, const CRect& rect, int iImage, BOOL bDrawFrame)
{
	CRect rectImage (CPoint (0, 0), CMenuImages::Size ());

	CRect rectFill = rect;
	rectFill.top -= 2;

	pDC->FillRect (rectFill, &globalData.brBtnFace);

	CPoint point (
		rect.left + (rect.Width () - rectImage.Width ()) / 2,
		rect.top + (rect.Height () - rectImage.Height ()) / 2);

	CMenuImages::Draw (pDC, (CMenuImages::IMAGES_IDS) iImage, point);

	if (bDrawFrame)
	{
		pDC->Draw3dRect (rect,
			globalData.clrBtnHilite,
			globalData.clrBtnShadow);
	}
}
//****************************************************************************************
void CBCGPopupMenu::ShowAllCommands ()
{
	CBCGToolbarMenuButton* pParentMenuButton =
		DYNAMIC_DOWNCAST (CBCGToolbarMenuButton, m_pParentBtn);
	if (pParentMenuButton != NULL)
	{
		CBCGMenuBar::SetShowAllCommands ();

		// Play standard menu popup sound!
		::PlaySound (_T("MenuPopup"), NULL, SND_ASYNC | SND_NODEFAULT);

		ShowWindow (SW_HIDE);
		m_bShown = FALSE;

		if (m_bmpShadowRight.GetSafeHandle () != NULL)
		{
			m_bmpShadowRight.DeleteObject ();
		}
		
		if (m_bmpShadowBottom.GetSafeHandle () != NULL)
		{
			m_bmpShadowBottom.DeleteObject ();
		}

		InitMenuBar ();
		UpdateBottomWindows ();

		ShowWindow (SW_SHOWNOACTIVATE);

		if (pParentMenuButton->m_pWndParent != NULL 
			&& ::IsWindow (pParentMenuButton->m_pWndParent->m_hWnd))
		{
			pParentMenuButton->m_pWndParent->InvalidateRect (
				pParentMenuButton->Rect ());
			pParentMenuButton->m_pWndParent->UpdateWindow ();
		}
	}
}

⌨️ 快捷键说明

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