Click here to Skip to main content
Click here to Skip to main content

Docking Toolbars Side-By-Side

, 15 Dec 1999
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to dock toolbars side-by-side
  • Download demo project - 29 Kb
  • Sample Image - toolbar_docking.gif

    <!-- Article Starts -->

    There are many articles about docking toolbars, however, I felt that this was important enough to mention here. The same information can be found at Microsofts MSDN site, here it is in a nutshell: Add the following method to your CMainFrame class:

    void CMainFrame::DockControlBarLeftOf(CToolBar* Bar, CToolBar* LeftOf)
    {
    	CRect rect;
    	DWORD dw;
    	UINT n;
    	
    	// get MFC to adjust the dimensions of all docked ToolBars
    	// so that GetWindowRect will be accurate
    	RecalcLayout(TRUE);
    	
    	LeftOf->GetWindowRect(&rect);
    	rect.OffsetRect(1,0);
    	dw=LeftOf->GetBarStyle();
    	n = 0;
    	n = (dw&CBRS_ALIGN_TOP) ? AFX_IDW_DOCKBAR_TOP : n;
    	n = (dw&CBRS_ALIGN_BOTTOM && n==0) ? AFX_IDW_DOCKBAR_BOTTOM : n;
    	n = (dw&CBRS_ALIGN_LEFT && n==0) ? AFX_IDW_DOCKBAR_LEFT : n;
    	n = (dw&CBRS_ALIGN_RIGHT && n==0) ? AFX_IDW_DOCKBAR_RIGHT : n;
    	
    	// When we take the default parameters on rect, DockControlBar will dock
    	// each Toolbar on a seperate line. By calculating a rectangle, we
    	// are simulating a Toolbar being dragged to that location and docked.
    	DockControlBar(Bar,n,&rect);
    }
    

    Now, in your CMainFrame::OnCreate, instead of using DockControlBar, use DockControlBarLeftOf:

    	m_wndToolBar1.EnableDocking(CBRS_ALIGN_ANY);
    	m_wndToolBar2.EnableDocking(CBRS_ALIGN_ANY);
    	EnableDocking(CBRS_ALIGN_ANY);
    	DockControlBar(&m_wndToolBar1);
    	DockControlBarLeftOf(&m_wndToolBar2,&m_wndToolBar1);
    

    This will dock m_wndToolBar2 left of m_wndToolBar1.

    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

    Kirk Stowell
    CEO Codejock Software
    United States United States
    No Biography provided

    Comments and Discussions

     
    GeneralMy vote of 5 PinmemberRobert Kisel13-Mar-11 11:33 
    GeneralRe: My vote of 5 PinmemberAlbert Holguin3-Oct-13 11:08 
    GeneralThanks Pinmemberbusch2929-Aug-09 23:03 
    GeneralShow a button in pressed state using CMFCToolbar PinmemberFarhat Aisha24-Sep-08 23:43 
    QuestionHow to put Toolbar at customized position Pinmemberakira3222-Sep-08 23:58 
    I have 3 toolbars in the mainframe.
    I want to put them as below:
     
    [Toolbar1][Toolbar3]
    [Toolbar2]
     
    Please somebody tell me how to I do reach the goal?
    I just know the DockControlBar is the key and align flag(maybe).
    I have tried calculated the rect of toolbar to the position in top of MainFrame, but the toolbar look like to be aligned to top. Even I changed the toolbar's align flag to be bottom. The toolbar2 also cannot align to the toolbar1 to the left and side by side.
     
    source code from the website: (I just addd third toobar)
    http://www.codeproject.com/KB/toolbars/toolbar_docking.aspx
     
    [pre]
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
    return -1;

    //
    if (!m_wndToolBar1.Create(this) ||
    !m_wndToolBar1.LoadToolBar(IDR_MAINFRAME))
    {
    TRACE0("Failed to create toolbar\n");
    return -1; // fail to create
    }
     
    m_wndToolBar1.ModifyStyle(0, TBSTYLE_FLAT);
     
    //
    if (!m_wndToolBar2.Create(this) ||
    !m_wndToolBar2.LoadToolBar(IDR_WINDOW))
    {
    TRACE0("Failed to create toolbar\n");
    return -1; // fail to create
    }
     
    m_wndToolBar2.ModifyStyle(0, TBSTYLE_FLAT);
     
    //
    if (!m_wndToolBar3.Create(this) ||
    !m_wndToolBar3.LoadToolBar(IDR_WINDOW))
    {
    TRACE0("Failed to create toolbar\n");
    return -1; // fail to create
    }
     
    m_wndToolBar3.ModifyStyle(0, TBSTYLE_FLAT);
     
    //
    if (!m_wndStatusBar.Create(this) ||
    !m_wndStatusBar.SetIndicators(indicators,
    sizeof(indicators)/sizeof(UINT)))
    {
    TRACE0("Failed to create status bar\n");
    return -1; // fail to create
    }
     
    // TODO: Delete these three lines if you don't want the toolbar to
    // be dockable
    m_wndToolBar1.EnableDocking(CBRS_ALIGN_ANY);
    m_wndToolBar2.EnableDocking(CBRS_ALIGN_ANY);
    m_wndToolBar3.EnableDocking(CBRS_ALIGN_ANY);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar1);
    DockControlBarLeftOf(&m_wndToolBar2,&m_wndToolBar1);
    DockControlBarLeftOf(&m_wndToolBar3,&m_wndToolBar2);
     
    return 0;
    }
     
    void CMainFrame::DockControlBarLeftOf(CToolBar* Bar, CToolBar* LeftOf)
    {
    CRect rect;
    DWORD dw;
    UINT n;

    // get MFC to adjust the dimensions of all docked ToolBars
    // so that GetWindowRect will be accurate
    RecalcLayout(TRUE);

    LeftOf->GetWindowRect(&rect);
    rect.OffsetRect(1,0);
    dw=LeftOf->GetBarStyle();
    n = 0;
    n = (dw&CBRS_ALIGN_TOP) ? AFX_IDW_DOCKBAR_TOP : n;
    n = (dw&CBRS_ALIGN_BOTTOM && n==0) ? AFX_IDW_DOCKBAR_BOTTOM : n;
    n = (dw&CBRS_ALIGN_LEFT && n==0) ? AFX_IDW_DOCKBAR_LEFT : n;
    n = (dw&CBRS_ALIGN_RIGHT && n==0) ? AFX_IDW_DOCKBAR_RIGHT : n;

    // When we take the default parameters on rect, DockControlBar will dock
    // each Toolbar on a seperate line. By calculating a rectangle, we
    // are simulating a Toolbar being dragged to that location and docked.
    DockControlBar(Bar,n,&rect);
    }
    [/pre]
    GeneralThe same with CMFCMenuBar Pinmemberbharath jois21-Aug-08 2:35 
    GeneralThat's what i want PinmemberXiong Shijie19-Sep-07 6:17 
    GeneralRe: That's what i want PinmemberThornets5-Aug-08 1:36 
    QuestionHi(I want to dock ToolBar To its original position ) [modified] Pinmembertupi_kasvala8-Sep-07 1:27 
    QuestionHow to remove the empty space in front of toolbar2 if uncheck view->toolbar Pinmemberjd0120-Nov-06 13:20 
    GeneralBeautiful Pinmembernadiric31-Aug-06 16:04 
    GeneralToolBar 1 not displayed properly PinmemberAbhi Lahare22-May-06 20:18 
    QuestionHow to control the position of the docking toolbar if the main frame window is resized? PinmemberMary Gao12-Apr-05 13:49 
    AnswerRe: How to control the position of the docking toolbar if the main frame window is resized? Pinmembermark-w20-Dec-05 16:48 
    GeneralRe: How to control the position of the docking toolbar if the main frame window is resized? Pinmemberjd0122-Nov-06 8:20 
    GeneralOdd Artifacts PinmemberBob Ciora30-Jan-05 5:52 
    GeneralThanks! PinmemberBob Ciora28-Jan-05 9:49 
    GeneralClasses Derived from CToolBar PinsussNoahNNFN11-Jan-05 7:59 
    GeneralRe: Classes Derived from CToolBar Pinmembericaro14-Jun-06 10:33 
    Generalremoving border/seperator Pinmembersocall0113-Dec-04 9:26 
    GeneralRe: removing border/seperator PinmemberLars [Large] Werner17-Mar-05 3:41 
    GeneralRe: removing border/seperator PinsussAnonymous12-Jul-05 18:59 
    GeneralRe: removing border/seperator PinmemberAlbert Holguin3-Oct-13 11:06 
    GeneralThanks alot! Pinmemberipichet19-Oct-04 1:27 
    GeneralDisplaying the Toolbar 2 PinmemberAudalio_Jr12-May-04 7:14 
    GeneralRe: Displaying the Toolbar 2 PinmemberDave_B24-Aug-04 22:41 
    GeneralRe: Displaying the Toolbar 2 PinmemberKJ Dugan21-Nov-04 4:05 
    Generalsimple and easy, thanks! PinmemberJohn Wong9-Sep-02 10:57 
    QuestionHow to Docking More than Three Toolbars Side-By-Side?? PinmemberAnonymous5-Jun-02 19:02 
    AnswerRe: How to Docking More than Three Toolbars Side-By-Side?? Pinmembermjwilliamson19-Sep-03 4:17 
    GeneralRe: How to Docking More than Three Toolbars Side-By-Side?? PinmemberPMartos26-Apr-04 7:31 
    GeneralShort and sweet PinmemberFrank Wittmann11-Apr-02 23:14 
    GeneralThanks again! PinmemberJamie Hale22-Feb-02 8:46 
    GeneralThanks!! PinmemberRobJones17-Feb-02 11:10 
    GeneralFor CDialogBar PinmemberFalk23-Aug-01 7:02 
    QuestionHow can I dock a toolbar in the center of the application? PinsussDanielle15-Jan-00 7:28 
    AnswerRe: How can I dock a toolbar in the center of the application? Pinmembertoxcct30-Jun-05 6:04 

    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.

    | Advertise | Privacy | Mobile
    Web03 | 2.8.140721.1 | Last Updated 16 Dec 1999
    Article Copyright 1999 by Kirk Stowell
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid