实现步骤:
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);
}