MFC offers two classes to provide the functionality of the windows toolbar: CToolbar and CtoolBarCtrl. The CToolBar encapsulates much of its functionality of the standard toolbar control where as CToolBarCtrl offer a more substantive programming interface.

To create a toolbar the developer can either

1. Instantiate an object of the CToolbar class and call the CtoolBarCtrl::CreateEx member function to create the toolbar followed by the member function CtoolBarCtrl::LoadToolBar to load the toolbar resource.

2. Instantiate an object of the CtoolBarCtrl class and define a TBBUTTON structure to provide details about the individual buttons. The CtoolBarCtrl class also requires a bitmap resource containing images for the faces of the toolbar buttons.

Toolbar buttons are assigned command IDs and clicking a toolbar button sends a WM_COMMAND message to the parent windows where the button ID is linked relevant command handler.

In addition to buttons, windows Toolbars can also contain combo boxes, check boxes, and other non-push-button controls. MFC provides functions for hiding and displaying toolbars, saving and restoring toolbar states, and much more. 

For a full description of the CToolBar Class and associated member functions
https://docs.microsoft.com/en-us/cpp/mfc/reference/ctoolbar-class?view=msvc-160#createex

For a full description of the CToolBarCtrl Class and associated member functions
https://docs.microsoft.com/en-us/cpp/mfc/reference/ctoolbarctrl-class?view=msvc-160 


mfc-toolbar-image

The following short program creates a simple program with two buttons. Clicking either button will produce a message box.

#include <afxwin.h>
#include <afxext.h>
#include "resource.h"
class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};

class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
afx_msg void OnButton1Click();
afx_msg void OnButton2Click();
afx_msg void InitialiseToolBar();
DECLARE_MESSAGE_MAP()
CToolBar wndToolBar;
};

BOOL CSimpleApp::InitInstance()
{
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}


CMainFrame::CMainFrame()
{
Create(NULL, "MFC Toolbar demo",WS_OVERLAPPEDWINDOW ,CRect(25,25,500,250));
InitCommonControls();
InitialiseToolBar();
}

BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
ON_COMMAND(TB_BUTTON1,OnButton1Click)//responds to first button click
ON_COMMAND(TB_BUTTON2,OnButton2Click)//responds to second button click
END_MESSAGE_MAP()

CSimpleApp MFCApp1;

afx_msg void CMainFrame::OnButton1Click()
{
MessageBox("Button1 clicked","Toolbar demo",MB_OK);
} 
afx_msg void CMainFrame::OnButton2Click()
{
MessageBox("Button2 clicked","Toolbar demo",MB_OK);
} 
afx_msg void CMainFrame::InitialiseToolBar()
{
wndToolBar.CreateEx(this,TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE| CBRS_TOP);
wndToolBar.LoadToolBar(IDR_TOOLBAR1);//load toolbar from resources
}


Download