Click here to Skip to main content
16,021,710 members
Articles / Desktop Programming / MFC
Article

Docking Toolbars Side-By-Side

Rate me:
Please Sign up or sign in to vote.
4.90/5 (37 votes)
15 Dec 1999 259.3K   3.2K   59   40
Demonstrates how to dock toolbars side-by-side
  • Download demo project - 29 Kb
  • Sample Image - toolbar_docking.gif

    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


    Written By
    CEO Codejock Technologies, LLC
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
    This is a Organisation (No members)


    Comments and Discussions

     
    QuestionHow to Docking More than Three Toolbars Side-By-Side?? Pin
    5-Jun-02 19:02
    suss5-Jun-02 19:02 
    AnswerRe: How to Docking More than Three Toolbars Side-By-Side?? Pin
    mjwilliamson19-Sep-03 4:17
    mjwilliamson19-Sep-03 4:17 
    GeneralRe: How to Docking More than Three Toolbars Side-By-Side?? Pin
    PMartos26-Apr-04 7:31
    PMartos26-Apr-04 7:31 
    GeneralShort and sweet Pin
    Lakitu11-Apr-02 23:14
    Lakitu11-Apr-02 23:14 
    GeneralThanks again! Pin
    Jamie Hale22-Feb-02 8:46
    Jamie Hale22-Feb-02 8:46 
    GeneralThanks!! Pin
    RobJones17-Feb-02 11:10
    RobJones17-Feb-02 11:10 
    GeneralFor CDialogBar Pin
    23-Aug-01 7:02
    suss23-Aug-01 7:02 
    QuestionHow can I dock a toolbar in the center of the application? Pin
    danielle15-Jan-00 7:28
    danielle15-Jan-00 7:28 
    I really like your code.

    I have two arrows (back and forward) on a toolbar line. Currently they are both located to the far left side of the screen. I can pick on the toolbar and re-position it to the center of the center, but if I terminate the program and then re-execute it, the toolbar is still positioned to the left of the screen.

    My question is: how can I dock a toolbar in the top, center of the application, rather than in the top left?

    Please, any response you can give me will be greatly appreciated.

    Sincerely,
    Daniell
    AnswerRe: How can I dock a toolbar in the center of the application? Pin
    toxcct30-Jun-05 6:04
    toxcct30-Jun-05 6:04 

    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.