CommandBars Articles and Tutorials

Add a splitter popup button that doesn't disappear

Author: Kirk Stowell
Platform: Visual C++ MFC

If you have created a splitter popup button as shown below, it will disappear when the ‘Reset Toolbar’ button is clicked.

pStandardBar->GetControls()->Add(xtpControlSplitButtonPopup, ID_BUTTON_POPUP);

If you use Customizable toolbars you must avoid dynamic creation of controls. You must use the ‘Control Sub classing’ technique. To add a splitter button do the following:

1. Add the button to your toolbar resource using the Visual Studio resource editor like so:

Splitter Button

2. Using the resource editor, create a Popup menu with same command id as the toolbar button you just added.

3. Add the command handler ON_XTP_CREATECONTROL() to your CMainFrame message map.

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
...
//}}AFX_MSG_MAP
ON_XTP_CREATECONTROL()
END_MESSAGE_MAP()

int CMainFrame::OnCreateControl(LPCREATECONTROLSTRUCT lpCreateControl)
{
    if (lpCreateControl->bToolBar)
    {
        if (lpCreateControl->nID == ID_BUTTON_POPUP)
        {
            lpCreateControl->controlType = xtpControlSplitButtonPopup;
            return TRUE;
        }
    }
    return FALSE;
}

4. Add the following line to your header file for CMainFrame:

afx_msg int OnCreateControl(LPCREATECONTROLSTRUCT lpCreateControl);

Application screen shot showing splitter popup button:

Splitter Button