Click here to Skip to main content
Licence 
First Posted 15 May 2002
Views 128,614
Bookmarked 61 times

Toolbar in splitter window pane

By | 15 May 2002 | Article
How to add a docking toolbar to a splitter window pane

Sample Image - image002.jpg

Introduction

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),
                           pContext);

     // Create the view using a CFrameWnd derived class.
     m_wndSplit.CreateView(0,1,RUNTIME_CLASS(CSplitFrame), CSize(175, 500), 
                           pContext);
 
     // Activate view pane.
     SetActiveView((CView *) m_wndSplit.GetPane(0,0));
 
     RecalcLayout();
 
     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.
    pview->SendMessage(WM_INITIALUPDATE);
    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
        | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
        !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    {
        TRACE0("Failed to create toolbar\n");
        return -1;      // fail to create
    }

    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
    m_wndToolBar.SetBorders(3, 3, 3, 3);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);    
    return 0;
}

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

License

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

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCommercial use of code Pinmemberit@controp.co.il20:01 4 Jun '11  
GeneralRemove lines to link to document Pinmemberfannyc6:08 4 Aug '06  
QuestionHow the two cview communicate? PinmemberAwinHuang0:05 14 Apr '06  
AnswerRe: How the two cview communicate? Pinmemberjhwurmbach23:41 17 Aug '06  
GeneralDocument won't save on Close PinmemberPhil Fortier19:54 23 Sep '05  
GeneralA handle to the MENU of a drop down button PinmemberAlex Evans13:28 16 Nov '04  
GeneralNo correct message maps :( PinmemberDamos1:16 10 Oct '03  
GeneralSmall omission in your article PinmemberBabak Asadi9:04 17 Sep '03  
GeneralCFrameWnd resize problem PinmemberChris Losinger9:29 23 May '03  
GeneralFixed PinmemberChris Losinger10:14 23 May '03  
GeneralRe: Access Document from View PinmemberRoberto Rocco10:00 10 Mar '03  
GeneralRe: Access Document from View Pinmemberdongdks22:43 23 Jul '03  
GeneralToolBarControls PinmemberAnthonyWinters9:30 30 Oct '02  
GeneralRe: ToolBarControls PinmemberRidgeley Yu0:18 12 May '03  
GeneralRe: ToolBarControls PinmemberTobias Svensson0:16 13 Oct '03  
GeneralNo document pointer PinsussEdmund Vermeulen22:47 29 Sep '02  
GeneralFrame Window PinmemberTimbit13:39 27 Aug '02  
GeneralRe: Frame Window PinmemberTimbit18:28 27 Aug '02  
GeneralRe: Frame Window PinmemberWarren Gardner14:35 25 Nov '02  
GeneralRe: Frame Window - Possibly even easier PinmemberJohn Famiglietti8:24 29 Nov '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 16 May 2002
Article Copyright 2002 by Tom Shope
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid