MFC Tutorial on Toolbars

Toolbars are components of windows operating system with functionalities to invoke dialogs or other screens. They are usually used as a quick links in the place of menus. This article explains how to use a toolbar in an MFC Window based application.

MFC Tutorial – Adding a Toolbar:

Step 1: Create a MFC Window application as described in MFC Tutorial – Part I
Step 2: Click on the Menu –> Insert Resource –> Toolbar. Add two buttons for our purpose with IDs named as IDC_TBBUTTON1 and IDC_TBBUTTON2.
Step 3: Add a member variable in side the class MFC_Tutorial_Window as follows.

       CToolBar myBar;

Step 4: Add the new set of code as highlighted in the following example to the MFC Window Application.

MFC Tutorial – Toolbar Sample

#include <afxwin.h>
#include <afxext.h> // MFC extensions
#include "resource.h"

class MFC_Tutorial_Window :public CFrameWnd // Example window for CToolbar sample
{
   CToolBar myBar; //MFC CToolBar based Toolbar variable
public:
   MFC_Tutorial_Window()
   {
		 Create(NULL,"MFC Tutorial 11 CoderSource Window ToolBar example");
   }
  int OnCreate(LPCREATESTRUCT lpCreateStruct);
  void OnToolBarButton1();
  void OnToolBarButton2();

  DECLARE_MESSAGE_MAP()
};

   //MessageMap for MFC Toolbar Sample
   BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd)
       ON_WM_CREATE()
      ON_COMMAND(IDC_TBBUTTON1,OnToolBarButton1)
      ON_COMMAND(IDC_TBBUTTON2,OnToolBarButton2)
   END_MESSAGE_MAP()

   void MFC_Tutorial_Window::OnToolBarButton1()
   {
        MessageBox("Toolbar Button 1 clicked");
   }
   void MFC_Tutorial_Window::OnToolBarButton2()
   {
       MessageBox("Toolbar Button 2 clicked");
   }

   int MFC_Tutorial_Window::OnCreate(LPCREATESTRUCT lpCreateStruct)
   {
       if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
           return -1;

       if (!myBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
          | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
      {
            MessageBox("Failed to create toolbarn");
            return -1; // fail to create
      }

     myBar.LoadToolBar(IDR_TOOLBAR1);
     myBar.EnableDocking(CBRS_ALIGN_ANY);
     EnableDocking(CBRS_ALIGN_ANY);
     DockControlBar(&myBar);
   }

   class MyApp :public CWinApp
   {
        MFC_Tutorial_Window *wnd; 
    public:
       BOOL InitInstance()
       {
          wnd = new MFC_Tutorial_Window();
          m_pMainWnd = wnd;
          m_pMainWnd->ShowWindow(1);
          return TRUE;
       }
   };

   MyApp theApp;

The MFC toolbar should be loaded during the WM_CREATE message. This is the time when the window of the class CFrameWnd gets created completely. This WM_CREATE message is sent immediately afterwards. This message is added to the Message Map with a handler.

MFC Tutorial – Important functions:

Apart from the WM_CREATE message, the 3 important functions to be known for a toolbar program are as follows.

CToolBar – Create :

This function creates the toolbar by calling the Win32 SDK CreateWindow function with relevant parameters.

CToolBar – LoadToolBar :

This function is used to load the toolbar resource (from .rc file) into the CToolBar class object.

CToolBar – EnableDocking:

This function enables a toolbar to be docked. The sides specified must match one of the sides enabled for docking in the destination frame window, or the tool bar cannot be docked to that frame window.

Please download the sample code from here.