Click here to Skip to main content
15,033,877 members
Articles / Desktop Programming / MFC
Posted 15 May 2002


65 bookmarked

Toolbar in splitter window pane

Rate me:
Please Sign up or sign in to vote.
4.74/5 (16 votes)
15 May 20021 min read
How to add a docking toolbar to a splitter window pane

Sample Image - image002.jpg


This brief article describes a simple way to add a toolbar inside one or more panes of a splitter window. Typically when you create a splitter window in the OnCreateClient method of your CMainFrame class, you supply CView derived classes to the CSplitterWnd::CreateView method. However, the class supplied does not need to be a CView derived class. It can be any CWnd derived class. If you look inside the MFC code for the CSplitterWnd class you will see that the class uses CWnd pointers not CView pointers. Methods in the CSplitterWnd class that deal with windows, use CWnd pointers. For instance, CSplitterWnd::GetPane returns a CWnd. This allows us to use a CFrameWnd derived class instead of a CView derived class. Since the CFrameWnd class provides all the toolbar / docking support, we can easily add a toolbar to each splitter pane that is derived from CFrameWnd. Below is the OnCreateClient code from my sample application:

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
     m_wndSplit.CreateStatic(this, 1, 2);
     m_wndSplit.CreateView(0,0,pContext->m_pNewViewClass, CSize(175, 500),

     // Create the view using a CFrameWnd derived class.
     m_wndSplit.CreateView(0,1,RUNTIME_CLASS(CSplitFrame), CSize(175, 500), 
     // Activate view pane.
     SetActiveView((CView *) m_wndSplit.GetPane(0,0));
     return TRUE;
     // return CFrameWnd::OnCreateClient(lpcs, pContext);

Now, your CFrameWnd derived class must do two things. First it has to create your view class in the OnCreateClient method. My sample code uses a CFormView derived class:

BOOL CSplitFrame::OnCreateClient(LPCREATESTRUCT lpcs, 
                                  CCreateContext* pContext) 
    CSplitView *pview;

    // Create a context.
    CCreateContext context;
    pContext = &context;

    // Assign custom view.
    pContext->m_pNewViewClass = RUNTIME_CLASS(CSplitView);

    // Create the view.
    pview = (CSplitView *) CreateView(pContext, AFX_IDW_PANE_FIRST);
    if (pview == NULL)
        return FALSE;

    // Notify the view.
    SetActiveView(pview, FALSE);

    return TRUE;

The second thing your CFrameWnd derived class must do is create the toolbar that will appear in the splitter pane:

int CSplitFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, 
                              WS_CHILD | WS_VISIBLE | CBRS_TOP
        TRACE0("Failed to create toolbar\n");
        return -1;      // fail to create

    m_wndToolBar.SetBorders(3, 3, 3, 3);
    return 0;

That's it! Now you have a fully dockable toolbar for your splitter window pane. Easy!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Tom Shope
Web Developer
United States United States
Graduate of University of Texas with B.S. in Computer Engineering. 14 years software development experience. Currently a software developer for Storcomm Inc. a medical imaging solutions provider in Jacksonville Florida.

Comments and Discussions

GeneralCommercial use of code Pin 20:01 20:01 
GeneralRemove lines to link to document Pin
fannyc4-Aug-06 6:08
Memberfannyc4-Aug-06 6:08 
QuestionHow the two cview communicate? Pin
AwinHuang14-Apr-06 0:05
MemberAwinHuang14-Apr-06 0:05 
AnswerRe: How the two cview communicate? Pin
jhwurmbach17-Aug-06 23:41
Memberjhwurmbach17-Aug-06 23:41 
GeneralDocument won't save on Close Pin
Phil Francis23-Sep-05 19:54
MemberPhil Francis23-Sep-05 19:54 
GeneralA handle to the MENU of a drop down button Pin
Alex Evans16-Nov-04 13:28
MemberAlex Evans16-Nov-04 13:28 
GeneralNo correct message maps :( Pin
Damos10-Oct-03 1:16
MemberDamos10-Oct-03 1:16 
GeneralSmall omission in your article Pin
Babak Asadi17-Sep-03 9:04
MemberBabak Asadi17-Sep-03 9:04 
GeneralCFrameWnd resize problem Pin
Chris Losinger23-May-03 9:29
professionalChris Losinger23-May-03 9:29 
GeneralFixed Pin
Chris Losinger23-May-03 10:14
professionalChris Losinger23-May-03 10:14 
GeneralRe: Access Document from View Pin
Roberto Rocco10-Mar-03 10:00
MemberRoberto Rocco10-Mar-03 10:00 
GeneralRe: Access Document from View Pin
dongdks23-Jul-03 22:43
Memberdongdks23-Jul-03 22:43 
GeneralToolBarControls Pin
Anthony Winters30-Oct-02 9:30
MemberAnthony Winters30-Oct-02 9:30 
GeneralRe: ToolBarControls Pin
Ridgeley Yu12-May-03 0:18
MemberRidgeley Yu12-May-03 0:18 
GeneralRe: ToolBarControls Pin
Tobias Svensson13-Oct-03 0:16
MemberTobias Svensson13-Oct-03 0:16 
GeneralNo document pointer Pin
Edmund Vermeulen29-Sep-02 22:47
MemberEdmund Vermeulen29-Sep-02 22:47 
GeneralFrame Window Pin
Timbit27-Aug-02 13:39
MemberTimbit27-Aug-02 13:39 
GeneralRe: Frame Window Pin
Timbit27-Aug-02 18:28
MemberTimbit27-Aug-02 18:28 
GeneralRe: Frame Window Pin
Warren Gardner25-Nov-02 14:35
MemberWarren Gardner25-Nov-02 14:35 
GeneralRe: Frame Window - Possibly even easier Pin
John Famiglietti29-Nov-02 8:24
MemberJohn Famiglietti29-Nov-02 8:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.