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 ,
int commandId,
LPCTSTR buttonText
)
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)
{
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);
}