1.首先设置欲弹出下拉菜单的工具条的样式,设置为下拉式
DWORD dwExStyle=TBSTYLE_EX_DRAWDDARROWS;
m_wndToolBar.GetToolBarCtrl().SendMessage(TB_SETEXTENDEDSTYLE,0,(LPARAM)dwExStyle);
DWORD dwStyle=m_wndToolBar.GetButtonStyle(m_wndToolBar.CommandToIndex(ID_CHANGE_TORECT));
dwStyle|=TBSTYLE_DROPDOWN;
m_wndToolBar.SetButtonStyle(m_wndToolBar.CommandToIndex(ID_CHANGE_TORECT),dwStyle);
2.然后响应下拉事件,当点击下拉箭头时,弹出下拉菜单
void CMainFrame::OnToolbarDropDown(NMHDR* pnmh,LRESULT* plRes)
{
CWnd *pWnd;
UINT nID;
NMTOOLBAR* pnmtb=(NMTOOLBAR*)pnmh;
// Switch on button command id''s.
switch (pnmtb->iItem)
{
case ID_CHANGE_TORECT:
pWnd = &m_wndToolBar;
nID = IDR_DRAWCLTYPE;
break;
default:
return;
}
// load and display popup menu
CMenu menu;
menu.LoadMenu(nID);
CMenu* pPopup = menu.GetSubMenu(7);
ASSERT(pPopup->SetMenuItemBitmaps(0,MF_BYPOSITION,&bitmap1,&bitmap1));
ASSERT(pPopup->SetMenuItemBitmaps(1,MF_BYPOSITION,&bitmap2,&bitmap2));
ASSERT(pPopup->SetMenuItemBitmaps(2,MF_BYPOSITION,&bitmap3,&bitmap3));
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);
}
3.最后在菜单响应事件中刷新图像,使之显示选中菜单的图像
CMainFrame* pfm= (CMainFrame*)AfxGetMainWnd();
UINT nID=0,nStyle=0;
int iImage=0;
CToolBarCtrl& toolctrl=pfm->m_wndToolBar.GetToolBarCtrl();
int index_rect=toolctrl.AddBitmap(1,IDB_BITMAP1);
int index=pfm->m_wndToolBar.CommandToIndex(ID_CHANGE_TORECT);
pfm->m_wndToolBar.GetButtonInfo(index,nID,nStyle,iImage);
pfm->m_wndToolBar.SetButtonInfo(index,nID,nStyle,index_rect);