Share via


Using Drop-Down Buttons in a Toolbar Control

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Using Drop-Down Buttons in a Toolbar Control.

In addition to standard push buttons, a toolbar can also have drop-down buttons. A drop-down button is usually indicated by the presence of an attached down arrow.

Note

The attached down arrow will appear only if the TBSTYLE_EX_DRAWDDARROWS extended style has been set.

When the user clicks on this arrow (or the button itself, if no arrow is present), a TBN_DROPDOWN notification message is sent to the parent of the toolbar control. You can then handle this notification and display a pop-up menu; similar to the behavior of Internet Explorer.

The following procedure illustrates how to implement a drop-down toolbar button with a pop-up menu:

To implement a drop-down button

  1. Once your CToolBarCtrl object has been created, set the TBSTYLE_EX_DRAWDDARROWS style, using the following code:

          m_ToolBarCtrl.SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);
    
  2. Set the TBSTYLE_DROPDOWN style for any new (InsertButton or AddButtons) or existing (SetButtonInfo) buttons that will be drop-down buttons. The following example demonstrates modifying an existing button in a CToolBarCtrl object:

          TBBUTTONINFO tbi;
    
          tbi.dwMask = TBIF_STYLE;
          tbi.cbSize = sizeof(TBBUTTONINFO);
          m_ToolBarCtrl.GetButtonInfo(0, &tbi);
          tbi.fsStyle |= TBSTYLE_DROPDOWN;
          m_ToolBarCtrl.SetButtonInfo(0, &tbi);
    
  3. Add a TBN_DROPDOWN handler to the parent class of the toolbar object.

       ON_NOTIFY(TBN_DROPDOWN, IDC_TOOLBAR1, &CMyDialog::OnTbnDropDownToolBar1)
    
  4. In the new handler, display the appropriate popup menu. The following code demonstrates one method:

    void CMyDialog::OnTbnDropDownToolBar1(NMHDR* pNMHDR, LRESULT* pResult)
    {
       LPNMTOOLBAR pToolBar = reinterpret_cast<LPNMTOOLBAR>(pNMHDR);
       ClientToScreen(&(pToolBar->rcButton)); // TrackPopupMenu uses screen coords
    
       CMenu menu;
       VERIFY(menu.LoadMenu(IDR_MENU1));
       CMenu* pPopup = menu.GetSubMenu(0);
       if (NULL != pPopup)
       {
          pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
             pToolBar->rcButton.left, pToolBar->rcButton.bottom, this);
       }
    
       *pResult = 0;
    }
    

See Also

Using CToolBarCtrl
Controls