Add a toolbar to an MFC application
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Add a toolbar to an MFC application

  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Add a toolbar to an MFC application

    Can someone please direct me to a tutorial of how to add a dockable toolbar to an MFC application with VStudio 2008/2010? I mean, a simple, beginner's tutorial ... I have searched this site and the web and it seems it is assumed that everyone already knows how to do this very simple task. Is it the same as adding one in Vstudio 6? (LOL yes, I just graduated from VC6 to 2008/2010).

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Add a toolbar to an MFC application

    Generally a toolbar is included in an MFC application. Perhaps you could build a simple project from the wizard, then see what is different.

  3. #3
    Join Date
    Apr 2010
    Posts
    3

    Re: Add a toolbar to an MFC application

    I want several more special purpose dockable toolbars, not just the canned one.

  4. #4
    Join Date
    Apr 2010
    Posts
    3

    Re: Add a toolbar to an MFC application

    Good idea though ...

  5. #5
    Join Date
    Nov 2007
    Posts
    613

    Re: Add a toolbar to an MFC application

    There are many topics in the MSDN related to certain aspects related to the toolbars but I don't know any tutorial to deal with ALL the problems related to them. If you would be a little more specific, maybe I could point to something

    Generally speaking, you don't need to know everything about them, you will use only a small fraction of all the features, depending on your intentions.

    To add multiple toolbars to a MFC appplication, do the followings (we will add 4 additional toolbars):

    1. Use the resource editor to create the toolbar resources

    2. Add four CToolBar variables to your CMainFrame class, tb1, tb2, tb3, tb4.

    3. In the CMainFrame::OnCreate function, AFTER the call to the base class CMDIFrameWnd::OnCreate function, add the following code:
    Code:
    if (!tb1.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !tb1.LoadToolBar(IDR_TB1))
    {
    TRACE0("Failed to create toolbar TB1\n");
    return -1; // fail to create
    }
    if (!tb2.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !tb2.LoadToolBar(IDR_TB2))
    {
    TRACE0("Failed to create toolbar TB2\n");
    return -1; // fail to create
    }
    if (!tb3.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !tb3.LoadToolBar(IDR_TB3))
    {
    TRACE0("Failed to create toolbar TB3\n");
    return -1; // fail to create
    }
    if (!tb4.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !tb4.LoadToolBar(IDR_TB4))
    {
    TRACE0("Failed to create toolbar TB4\n");
    return -1; // fail to create
    }
    4. At the END of the same function, add the following code:
    Code:
    tb1.EnableDocking(CBRS_ALIGN_ANY);
    tb2.EnableDocking(CBRS_ALIGN_ANY);
    tb3.EnableDocking(CBRS_ALIGN_ANY);
    tb4.EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&tb1, AFX_IDW_DOCKBAR_BOTTOM);
    DockControlBar(&tb2, AFX_IDW_DOCKBAR_LEFT);
    DockControlBar(&tb3, AFX_IDW_DOCKBAR_RIGHT);
    FloatControlBar(&tb4, CPoint(100, 100));
    You'll get an application with one toolbar docked to each side of the window and one floating toolbar.

    The whole CMainFrame::OnCreate function should look like this:
    Code:
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CMDIFrameWnd::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
    }
    if (!tb1.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !tb1.LoadToolBar(IDR_TB1))
    {
    TRACE0("Failed to create toolbar TB1\n");
    return -1; // fail to create
    }
    if (!tb2.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !tb2.LoadToolBar(IDR_TB2))
    {
    TRACE0("Failed to create toolbar TB2\n");
    return -1; // fail to create
    }
    if (!tb3.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !tb3.LoadToolBar(IDR_TB3))
    {
    TRACE0("Failed to create toolbar TB3\n");
    return -1; // fail to create
    }
    if (!tb4.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !tb4.LoadToolBar(IDR_TB4))
    {
    TRACE0("Failed to create toolbar TB4\n");
    return -1; // fail to create
    }
     
    if (!m_wndStatusBar.Create(this) ||
    !m_wndStatusBar.SetIndicators(indicators,
    sizeof(indicators)/sizeof(UINT)))
    {
    TRACE0("Failed to create status bar TB4\n");
    return -1; // fail to create
    }
    // TODO: Delete these three lines if you don't want the toolbar to be dockable
    EnableDocking(CBRS_ALIGN_ANY);
    m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
    tb1.EnableDocking(CBRS_ALIGN_ANY);
    tb2.EnableDocking(CBRS_ALIGN_ANY);
    tb3.EnableDocking(CBRS_ALIGN_ANY);
    tb4.EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);
    DockControlBar(&tb1, AFX_IDW_DOCKBAR_BOTTOM);
    DockControlBar(&tb2, AFX_IDW_DOCKBAR_LEFT);
    DockControlBar(&tb3, AFX_IDW_DOCKBAR_RIGHT);
    FloatControlBar(&tb4, CPoint(200,200));
    return 0;
    }
    Last edited by srelu; April 17th, 2010 at 03:59 AM.

  6. #6
    Join Date
    May 2010
    Posts
    1

    Re: Add a toolbar to an MFC application

    To srelu:
    In the example you give, the four toolbars have a COMMON windows ID, which will produce some problems?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center