Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How to create a toolbar using MFC?
:confused:
Posted
Updated 16-Jun-10 7:59am
v2
Comments
Sandeep Mewara 16-Jun-10 13:59pm    
What have you tried so far?
Niklas L 17-Jun-10 16:03pm    
Please read the edit in my answer. It points you to how to add a toolbar to a dialog. There are a few more steps you need to take to make it work.

1 solution

The following steps are for a SDI or MDI project prior to the "MFC Extension Pack" in VS 2008. After that there are more options.

1. Create a Toolbar resource in the Resource View, give it an ID, e.g. ID_TOOLBAR
2. Add the buttons you'd like.
3. If you want the toolbar in your main frame window or in a child frame window, create an instance of a CToolbar class, e.g.CToolBar m_toolbar; in CMainFrame or CChildFrame class.
4. Create the toolbar window in the frames OnCreate member like
C++
if (!m_toolbar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_toolbar.LoadToolBar(IDR_TOOLBAR))
{
    TRACE0("Failed to create toolbar\n");
    return -1;      // fail to create
}

m_toolbar.EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_toolbar);


If you want it placed somewhere else, like in a dialog, you have to do it another way. A bit more work but manageable. You might wanna have a look at this[^].

Also check the documentation[^]

Edit: The excellent article A Toolbar in the middle of elsewhere[^] describes very well how to add a toolbar to a dialog.
 
Share this answer
 
v3
Comments
Sweety Khan 17-Jun-10 10:42am    
i got these errors :(
error C3861: 'EnableDocking': identifier not found
error C3861: 'DockControlBar': identifier not found
Niklas L 17-Jun-10 11:39am    
Are you doing this in a CFrameWnd derived class or somewhere else? The answer to that is vital.
Sweety Khan 17-Jun-10 14:41pm    
i added this in my header file

CToolBar myBar;
public:
int OnCreate(LPCREATESTRUCT lpCreateStruct);
void OnToolBarButton1();
void OnToolBarButton2();

and i added this in .cpp file

BEGIN_MESSAGE_MAP(CtoolbarfDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_COMMAND(IDC_TBBUTTON1,OnToolBarButton1)
ON_COMMAND(IDC_TBBUTTON2,OnToolBarButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CtoolbarfDlg::OnToolBarButton1()
{

}
void CtoolbarfDlg::OnToolBarButton2()
{

}

int CtoolbarfDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::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))
{
TRACE0("Failed to create toolbar
");
return -1; // fail to create
}

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900