次の方法で共有


Using Drop-Down Buttons in a Toolbar Control

OverviewSample

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_DRAWDARROWS 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 popup menu; similar to the behavior of Internet Explorer.

The following procedure illustrates how to implement a drop-down toolbar button with a popup 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_wndToolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);
    
  2. Set the TBSTYLE_DROPDOWN style for any new ( or ) or existing () 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_wndToolBar.GetToolBarCtrl().GetButtonInfo(ID_EDIT_CUT, &tbi);
    tbi.fsStyle |= TBSTYLE_DROPDOWN;
    m_wndToolBar.GetToolBarCtrl().SetButtonInfo(ID_EDIT_CUT, &tbi);
    
  3. Add a WM_NOTIFY handler to the parent class of the toolbar object.

  4. In the new handler, check for the TBN_DROPDOWN notification, using the following code:

    #define lpnm   ((LPNMHDR)lParam)
    #define lpnmTB ((LPNMTOOLBAR)lParam)
    
       switch(lpnm->code)
       {
          case TBN_DROPDOWN:
             //drop down button was hit
             //handle appropriately
             . . .
             return FALSE; //indicates the TBN_DROPDOWN
                           //notification was handled.
       }
    
  5. If the TBN_DROPDOWN notification has been sent, display the appropriate popup menu. The following code demonstrates one method:

    CMenu menu;
    VERIFY(menu.LoadMenu(IDR_MENU1));
    CMenu* pPopup = menu.GetSubMenu(0);
    ASSERT(pPopup != NULL);
    pPopup->TrackPopupMenu(TPM_RIGHTALIGN |
       TPM_RIGHTBUTTON, x, y, this);
    

See Also   Windows Common Controls and MFC Classes