Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / MFC
Article

CStatic derived control which is a replacement for standard toolbar

Rate me:
Please Sign up or sign in to vote.
4.10/5 (15 votes)
16 Jul 2003 70.9K   2.1K   26   8
ToolBar control with cool look and easy implementation.

Sample Image - toolbarexctrl.jpg

Introduction

This is a replacement for a standard ToolBar. This control is derived from CStatic to allow you an easy drop in, also the usage is very simple. It also features tooltips for every button and any size icons for buttons.

Usage

Creating

In your dialog template, place a CStatic control and make sure to check the Notify check box. Add a member to you dialog for this CStatic and specify CtoolBarEx as it's type.

Adding buttons

In your dialog InitDialog function, add buttons to the toolbar like this:

m_toolBar.AddButton((HICON)LoadImage(AfxGetInstanceHandle( ),
  MAKEINTRESOURCE(IDI_ICON2),IMAGE_ICON,16,16,0),300,"Test Button 1");
 m_toolBar.AddButton((HICON)LoadImage(AfxGetInstanceHandle( ),
  MAKEINTRESOURCE(IDI_ICON3),IMAGE_ICON,16,16,0),301,"Test Button 2");
 m_toolBar.AddButton((HICON)LoadImage(AfxGetInstanceHandle( ),
  MAKEINTRESOURCE(IDI_ICON2),IMAGE_ICON,16,16,0),302,"Test Button 3");
 m_toolBar.AddSeparator();
 m_toolBar.AddButton((HICON)LoadImage(AfxGetInstanceHandle( ),
  MAKEINTRESOURCE(IDI_ICON5),IMAGE_ICON,16,16,0),303,"Test Button 4");
 m_toolBar.AddButton((HICON)LoadImage(AfxGetInstanceHandle( ),
  MAKEINTRESOURCE(IDI_ICON2),IMAGE_ICON,16,16,0),304,"Test Button 5");

The AddButton function receives these parameters:

CToolBarEx::AddButon( 
HICON hIcon ,  // The Icon for this button 
int commandId, // The command id of this button
               // (will be used in message handler) 
LPCTSTR buttonText  // The text of the tooltip
                    // that will be displayed for this button 
) 

Optionally you can:

Set the button size:
CToolBarEx::SetButtonSize(CSize size);
Set button spacing:
CToolBarExCtrl::SetButtonSpacing(CSize spacing);
Add a separator:
CToolBarExCtrl::AddSeparator();
Remove buttons:
CToolBarExCtrl::RemoveButton(int nIndex, int commandId);

Use either the button index or commandId, set the one that is not in use to -1.

Set the background color:
CToolBarExCtrl::SetBackGroundColor(COLORREF color);
Enable or disable buttons:
CToolBarExCtrl::EnableButton(int nButtonId, bool bEnable);

TRUE to enable, FALSE to disable.


Reacting to button clicks

In your dialog add a handler to WM_COMMAND message. The button commandId is passed in wParam. Example:

BOOL CCToolBarExDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
 // TODO: Add your specialized code here and/or call the base class
 switch(wParam)
 {
 case 300:
  AfxMessageBox("Button1");
  break;
 case 301:
  AfxMessageBox("Button2");
  break;
 case 302:
  AfxMessageBox("Button3");
  break;
 case 303:
  AfxMessageBox("Button4");
  break;
 case 304:
  AfxMessageBox("Button5");
  break;
 case 305:
  AfxMessageBox("Button6");
  break;
 }
 return CDialog::OnCommand(wParam, lParam);
}

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
Software Developer (Senior) RDV Systems
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice idea, tooltip does not show Pin
s196675m15-Oct-07 7:15
s196675m15-Oct-07 7:15 
Generalmemory leak fixed Pin
shreejan19-Jan-06 20:00
shreejan19-Jan-06 20:00 
Change destructor of CToolBarExCtrl

CToolBarExCtrl::~CToolBarExCtrl()
{
for (int i = 0; i < buttons.GetSize(); i++)
{
DestroyIcon(buttons[i].icon);
delete [] buttons[i].tooltip;
}

buttons.RemoveAll();
}
Questionhow to use in sdi or mdi Pin
liuliu17-Oct-05 17:21
liuliu17-Oct-05 17:21 
AnswerRe: how to use in sdi or mdi Pin
Alex Hazanov18-Oct-05 1:48
Alex Hazanov18-Oct-05 1:48 
Generalproblem in property list ocx Pin
moshtagh18-Mar-06 3:02
moshtagh18-Mar-06 3:02 
GeneralRe: problem in property list ocx Pin
Alex Hazanov20-Mar-06 10:49
Alex Hazanov20-Mar-06 10:49 
GeneralMemory leak. Pin
sayherman19-Feb-05 3:28
sayherman19-Feb-05 3:28 
GeneralNo any target of applicability Pin
vgrigor22-Jul-03 23:22
vgrigor22-Jul-03 23:22 

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.