65.9K
CodeProject is changing. Read more.
Home

CStatic derived control which is a replacement for standard toolbar

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.10/5 (13 votes)

Jul 17, 2003

viewsIcon

71717

downloadIcon

2125

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);
}