实现步骤:
1、toolbar在CMainFrame::OnCreate()中被创建之后,扩展Toolbar的样式使其能够映射箭头按钮的消息。
DWORD dwExStyle = TBSTYLE_EX_DRAWDDARROWS;
m_wndToolBar.GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE, 0, (LPARAM)dwExStyle);
2、将箭头加到指定的按钮中,通过SetButtonStyle()方法实现:
DWORD dwStyle = m_wndToolBar.GetButtonStyle(m_wndToolBar.CommandToIndex(ID_SEL_MODE));
dwStyle |= TBSTYLE_DROPDOWN;
m_wndToolBar.SetButtonStyle(m_wndToolBar.CommandToIndex(ID_SEL_MODE), dwStyle);
m_wndToolBar.SetButtonStyle(m_wndToolBar.CommandToIndex(ID_VIEW_MODE), dwStyle);
3、加入箭头按钮的消息映射:
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
ON_NOTIFY(TBN_DROPDOWN, AFX_IDW_TOOLBAR, OnToolbarDropDown)
END_MESSAGE_MAP()
声明消息响应函数
protected:
afx_msg void OnToolbarDropDown(NMTOOLBAR* pnmh, LRESULT* plRes);
消息响应函数:
void CMainFrame::OnToolbarDropDown(NMTOOLBAR* pnmtb, LRESULT *plr)
{
CWnd *pWnd;
UINT nID;
// Switch on button command id's.
switch (pnmtb->iItem)
{
case ID_SEL_MODE:
pWnd = &m_wndToolBar;
nID = IDR_SEL_MODE;
break;
case ID_VIEW_MODE:
pWnd = &m_wndToolBar;
nID = IDR_VIEW_MODE;
break;
default:
return;
}
// load and display popup menu
CMenu menu;
menu.LoadMenu(nID);
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup);
CRect rc;
pWnd->SendMessage(TB_GETRECT, pnmtb->iItem, (LPARAM)&rc);
pWnd->ClientToScreen(&rc);
pPopup->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL,
rc.left, rc.bottom, this, &rc);
}