Click here to Skip to main content
15,893,487 members
Articles / Desktop Programming / MFC

Adding Text to a Docking Toolbar

Rate me:
Please Sign up or sign in to vote.
4.63/5 (14 votes)
16 May 20011 min read 154K   1.7K   34   16
This article demonstrates an easy way to add text to a docking tool bar

Introduction

This article demonstrates an easy way to add text  to a docking tool bar. I needed to add text to the toolbar for my dBase Explorer project (visit my web site for detail information: http://www.codearchive.com/~dbase/). Although I found some articles on it in the code projects site. Most of them are difficult to implement and you need to add a lot of coding or even a new class. Finally I found the easy way to add text to a toolbar. So I would like to share it with the rest of the world. This article shows how you can add text to a toolbar only by adding a few lines of code.

Image 1

In order to add text to a toolbar you need to declare a member variables type CToolBar to the CMainFrame class as shown below:

//
// Any source code blocks look like this
class CMainFrame : public CFrameWnd
{
	
protected: // create from serialization only
	CMainFrame();
	DECLARE_DYNCREATE(CMainFrame)

protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;

...
};

If you use App Wizard to create your application, the member variable will be declared for you so do you do not need to declared it by yourself. However, you have to call the SetButtonText function with two parameters. The first one is the index of the icon, and the second one is the actual text. The text will appear just below the icon. The index starts from 0. Each separator has also an index, so you have to count that too. The program listing below shows the index of each icon and also the text for each icon.

The complete listing of OnCreate function is given below:

//
//
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::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 (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

    	m_wndToolBar.SetButtonText(0,"New");
	m_wndToolBar.SetButtonText(1,"Open");
	m_wndToolBar.SetButtonText(2,"Save");
	m_wndToolBar.SetButtonText(4,"Cut");
	m_wndToolBar.SetButtonText(5,"Copy");
	m_wndToolBar.SetButtonText(6,"Paste");
	m_wndToolBar.SetButtonText(8,"Print");
	m_wndToolBar.SetButtonText(10,"About");
	m_wndToolBar.SetSizes(CSize(42,38),CSize(16,15));


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

	return 0;
}

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
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalmy Vote is 5 Pin
Schehaider_Aymen26-Dec-12 4:15
Schehaider_Aymen26-Dec-12 4:15 
GeneralCToolBarAutoSizeText Pin
Nathan Lewis15-Nov-06 18:28
Nathan Lewis15-Nov-06 18:28 
Questionshow text on the right side of icon? Pin
epubcn28-Sep-06 17:16
epubcn28-Sep-06 17:16 
AnswerRe: show text on the right side of icon? Pin
bob169728-Dec-06 3:58
bob169728-Dec-06 3:58 
GeneralNot the best EASY way... Pin
Douglas R. Keesler27-Dec-04 7:10
Douglas R. Keesler27-Dec-04 7:10 
QuestionAdding Left aligned text? Pin
Ning Cao2-Feb-04 1:25
Ning Cao2-Feb-04 1:25 
AnswerRe: text only toolbar Pin
Douglas R. Keesler26-Dec-04 21:49
Douglas R. Keesler26-Dec-04 21:49 
GeneralRe: text only toolbar Pin
Priya_Sundar27-Jan-09 0:57
Priya_Sundar27-Jan-09 0:57 
drake28 wrote:
so why not just use a dialog bar and place button controls on it. You can owner draw them if you want to customize their appearance.



Thanks for that valuable suggestion! Didnt know that such a possibility existed...

Priya Sundar

GeneralAdjust the size of the toolbar after adding button text Pin
stokos16-Dec-03 11:58
stokos16-Dec-03 11:58 
QuestionWhere is my text? Pin
fenddy17-Aug-03 21:06
fenddy17-Aug-03 21:06 
GeneralHey MR. Lame, you are Lame Pin
20-Dec-01 9:25
suss20-Dec-01 9:25 
GeneralRe: Hey MR. Lame, you are Lame Pin
liquidShit10-Jun-04 2:30
sussliquidShit10-Jun-04 2:30 
GeneralMagic numbers and other questions Pin
29-May-01 14:19
suss29-May-01 14:19 
GeneralNo Need to Hard Code the Text Either Pin
18-May-01 10:30
suss18-May-01 10:30 
GeneralRe: No Need to Hard Code the Text Either Pin
Rui Dias Lopes6-Jan-02 12:22
Rui Dias Lopes6-Jan-02 12:22 
GeneralNo need to hard code the indexes Pin
Markus Axelsson17-May-01 22:28
Markus Axelsson17-May-01 22:28 

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.