Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CStatic derived control which is a replacement for standard toolbar

0.00/5 (No votes)
16 Jul 2003 1  
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