News | Articles | Libraries | Developer Tools | Books | Forum Links | Search   
Sections:
 

QA: How to add dropdown buttons in the CCeCommandBar?

By Louis-Xavier Vignal, February 01, 2002.
Print version

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:

Discuss

Discuss this article. Here you can write your comments and read comments of other developers.
Rate this article:     Poor Excellent    
 12345 
© 2001-2005 Pocket PC Developer Network, a division of Spb Software House