QA: How to add dropdown buttons in the CCeCommandBar?
Louis-Xavier Vignal (lxvignal@yahoo.fr), February 01, 2002.
Question
I have created a menu bar using the MFC class CCeCommandBar.
Now, I would like to insert a button that uses the TBSTYLE_DROPDOWN style.
How can I do it and then receive the TBN_DROPDOWN notification message
sent by the toolbar control when the user clicks the button?
Answer
To add dropdown style to a certain button you should delete it, add TBSTYLE_DROPDOWN
style and insert again. To show dropdown menu you should handle TBN_DROPDOWN
notification message in OnNotify function.
Here is a sample based in a dialog:
CRect rect;
void AddDropDown(CCeCommandBar *pCb, int nButtonIndex)
{
CToolBarCtrl *pToolbar = &pCb->GetToolBarCtrl();
TBBUTTON but;
pToolbar->GetButton(nButtonIndex, &but);
pToolbar->DeleteButton(nButtonIndex);
but.fsStyle = but.fsStyle | TBSTYLE_DROPDOWN;
pToolbar->InsertButton(nButtonIndex, &but);
pCb->SetSizes(CSize(23,21), CSize(16,15));
pToolbar->GetItemRect(nButtonIndex, &rect);
pToolbar->ClientToScreen(&rect);
};
BOOL CDropDownSampleDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
CenterWindow(GetDesktopWindow());
CCeCommandBar *pCommandBar = (CCeCommandBar*)m_pWndEmptyCB;
pCommandBar->LoadToolBar(IDR_TOOLBAR);
AddDropDown(pCommandBar, 1);
return TRUE;
}
It adds a dropdown arrow to the second button in the toolbar.
Finally, in order to receive the TBN_DROPDOWN notification, override
the OnNotify message handler. You can show a popup menu in the handler.
BOOL CDropDownSampleDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
NMTOOLBAR* lpnm = (NMTOOLBAR*) lParam;
if (lpnm->hdr.code == TBN_DROPDOWN) {
CMenu menu;
menu.LoadMenu(IDR_POPUP);
CMenu *pMenu = menu.GetSubMenu(0);
pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_BOTTOMALIGN, rect.left, rect.top-1, this, NULL);
}
return CDialog::OnNotify(wParam, lParam, pResult);
}
Sample project
You can download sample dialog based project with dropdown feature -
DropDownSample.zip (40 Kb).
Related resources:
-
http://www.pocketpcdn.com/sections/ui.html
Section: User Interface
-
http://www.pocketpcdn.com/articles/cmdbar_menu.html
QA: How do I get the command bar menu handle?
-
http://www.pocketpcdn.com/articles/second_commandbar.html
QA: How can I implement the second toolbar like the one in Pocket Word?
-
http://www.pocketpcdn.com/articles/updatecommandui.html
QA: Why ON_UPDATE_COMMAND_UI handlers get called again and again?
-
http://www.pocketpcdn.com/articles/dialogbar.html
QA: How can I use a command bar in a dialog?
-
http://www.pocketpcdn.com/articles/remove_new_button.html
QA: How to remove New button from command bar?