CToolBar

Win32++

CToolBar Class

Description

This is the class responsible for creating and managing a toolbar.  A toolbar is a control window that contains one or more buttons. Each button, when clicked by a user, sends a command message to the parent window. Typically, the buttons in a toolbar correspond to items in the application's menu, providing an additional and more direct way for the user to access an application's commands.

Refer to the documentation that ships with the Microsoft Windows Software Development Kit for more information on the use of toolbar controls.

CToolBar Members

CToolBar
CToolBar();
Constructor for CToolBar.
AddBitmap
virtual int AddBitmap(UINT ToolBarID);
Adds a bitmap to the toolbar.  See also SetImages. AddButton
virtual BOOL AddButton(UINT nID, BOOL bEnabled = TRUE);
Adds one button to the toolbar. AddButtons
BOOL AddButtons(UINT uNumButtons, LPTBBUTTON lpButtons) const;
Adds one or more buttons to the toolbar. AddString
int AddString(UINT nStringID) const;
Adds resource IDs to toolbar buttons. AddStrings
int AddStrings(LPCTSTR lpszStrings) const;
Adds strings to the toolbar's string pool. AutoSize
void Autosize() const;
Resizes the toolbar. CheckButton
void CheckButton(int idButton, BOOL fCheck) const;
Checks or unchecks a given button in a toolbar. CommandToIndex
int CommandToIndex(int idButton) const;
Returns the button index, given the command ID. Customize
void Customize() const;
Displays the Customize Toolbar dialog box. The parent must handle the TBN_QUERYINSERT and TBN_QUERYDELETE notifications for the Customize Toolbar dialog box to appear. DeleteButton
BOOL DeleteButton(int iButton) const;
Deletes a button from the toolbar DisableButton
BOOL DisableButton(int idButton) const;
Disables a toolbar button. EnableButton
BOOL EnableButton(int idButton) const;
Enables a toolbar button. GetButton
BOOL GetButton(int iButton, LPTBBUTTON lpButton) const;
Retrieves information about the specified button in the toolbar. GetButtonCount
int GetButtonCount() const;
Returns the number of buttons in the toolbar control. GetButtonSize
DWORD GetButtonSize() const;
Retrieves the current width and height of toolbar buttons, in pixels. GetButtonState
UINT GetButtonState(int idButton) const;
Returns the state of a toolbar button. GetButtonStyle
BYTE GetButtonStyle(int idButton) const
Returns the style of toolbar button. GetButtonText
LPCTSTR GetButtonText(int idButton) const;
Retrieves the display text of a button on the toolbar. GetCommandID
int GetCommandID(int iIndex) const;
Returns the command ID given the button index. GetDisabledImageList
HIMAGELIST GetDisabledImageList() const;
Retrieves the image list that the toolbar uses to display inactive buttons. GetHotImageList
HIMAGELIST GetImageList() const;
Retrieves the image list that the toolbar uses to display hot buttons. GetHotItem
int GetHotItem() const;
Retrieves the index of the hot button. GetImageList
HIMAGELIST GetImageList() const;
Retrieves the image list that the toolbar uses to display buttons in their default state. GetItemRect
CRect GetItemRect(int iIndex) const;
Returns the bounding rectangle of a button in a toolbar. GetMaxSize
CSize GetMaxSize() const;
Returns the SIZE required to contain the toolbar's buttons. GetPadding
DWORD GetPadding() const;
Retrieves the padding for the toolbar. GetRect
CRect GetRect(int idButton) const;
Retrieves the bounding rectangle for a specified toolbar button. GetRows
int GetRows() const;
Retrieves the number of rows of buttons in the toolbar. GetTextRows
int GetTextRows() const;
Retrieves the maximum number of text rows that can be displayed on a toolbar button. GetToolBarData
std::vector<UINT>& GetToolBarData() const
Returns a reference to the vector of resource IDs for toolbar buttons. GetToolTips
CToolTip* GetToolTips() const;
Retrieves the handle to the TooTip control, if any, associated with the toolbar. HasText
BOOL HasText() const;
Returns TRUE if the button has text. HideButton
BOOL HideButton(int idButton, BOOL fShow) const;
Hides or shows the specified toolbar button. HitTest
int HitTest() const;
Determines which button a point lies in a toolbar control. Indeterminate
BOOL Indeterminate(int idButton, BOOL fIndeterminate) const;
Sets or clears the indeterminate state of the specified toolbar button. InsertButton
BOOL InsetButton(int iButton, LPTBBUTTON lpButton) const;
Inserts a button in the toolbar. IsButtonHidden
BOOL IsButtonHidden(int idButton) const;
Determines whether the specified toolbar button is hidden. IsButtonHighlighted
BOOL IsButtonHighlighted(int idButton) const;
Checks the highlight state of the toolbar button. IsButtonIndeterminate
BOOL IsButtonIndeterminate(int idButton) const;
Determines whether the specified toolbar button is indeterminate. IsButtonPressed
BOOL IsButtonPressed(int idButton) const;
Determines whether the specified toolbar button is pressed. MapAccelerator
int MapAccelerator(TCHAR chAccel) const;
Determines the ID of the button that corresponds to the specified accelerator character. MarkButton
BOOL MarkButton(int idButton) const;
Sets the highlight state of a given button in a toolbar control. MoveButton
BOOL MoveButton(UINT uOldPos, UINT uNewPos) const;
Moves a button from one index to another. PressButton
BOOL PressButton(int idButton, BOOL fPress) const;
Presses or releases the specified toolbar button. ReplaceBitmap
virtual BOOL ReplaceBitmap(UINT NewToolBarID);
Replaces a toolbar's bitmap. See also SetImages. SaveRestore
void SaveRestore(BOOL fSave, TBSAVEPARAMS* ptbsp) const;
Initiates saving or restoring the toolbar's state. SetBitmap
virtual BOOL SetBitmap(UINT nID);
Sets a toolbar's bitmap.  See also SetImages. SetBitmapSize
BOOL SetBitmapSize(int cx, int cy) const;
Sets the size of the bitmapped images to be added to a toolbar.  Use this before adding the bitmap. SetButtonInfo
void SetButtonInfo(int idButton, int idButtonNew, int iImage, BYTE Style = 0, BYTE State = 0) const;
Use this to change the buttons image and ID. The ID must be changed for the image to be changed. SetButtonSize
BOOL SetBitmapSize(int cx, int cy) const;
Sets the size of the buttons to be added to a toolbar. SetButtonState
BOOL SetButtonState(int idButton, UINT State) const;
Sets the state of a toolbar button. SetButtonStyle
BOOL SetButtonStyle(int idButton, BYTE Style) const;
Sets the style of a toolbar button. SetButtonText
virtual BOOL SetButtonText(int idButton, LPCTSTR szText);
Sets the text for a toolbar button. SetButtonWidth
BOOL SetButtonWidth(int idButton, int nWidth) const;
Adjusts the width of a toolbar button after it is created. This is useful when replacing a button with a ComboBox or other control. SetCommandID
BOOL SetCommandID(int iIndex, int idButton) const;
Sets the Command ID of a toolbar button. SetDisabledImageList
HIMAGELIST SetDisableImageList(HIMAGELIST himlNewDisabled) const;
Sets the image list that the toolbar will use to display disabled buttons. SetDrawTextFlags
DWORD SetDrawTextFlags(DWORD dwMask, DWORD dwDTFlags) const;
Sets the text drawing flags for the toolbar. SetExtendedStyle
DWORD SetExtendedStyle(DWORD dwExStyle) const;
Sets the extended styles for the toolbar. SetHotImageList
HIMAGELIST SetHotImageList(HIMAGELIST himlNewHot) const;
Sets the image list that the toolbar will use to display hot buttons. SetHotItem
int SetHotItem(int iHot);
Sets the hot item in the toolbar. This message is ignored for toolbars that do not have the TBSTYLE_FLAT style. SetImageList
HIMAGELIST SetImageList(HIMAGELIST himlNew) const;
Sets the image list that the toolbar will use to display buttons that are in their default state. SetIndent
BOOL SetIndent(int iIndent) const;
Sets the indentation for the first toolbar button. SetMaxTextRows
BOOL SetMaxTextRows(int iMaxRows) const;
Sets the maximum number of text rows displayed on a toolbar button. SetPadding
BOOL SetPadding(int cx, int cy) const;
Sets the padding for the toolbar. SetToolTips
void SetToolTips(CToolTip* pToolTip) const;
Associates a ToolTip control with a toolbar.
Base class Members

For base class members, refer to the members of CWnd.

Remarks

Like all common controls, the toolbar control requires a parent window. This parent window is often a dialog, but simple windows can also be the parent window for a toolbar control.

The following code demonstrates how to display a ComboBoxEx control in a toolbar.  Before displaying the control over the 'File Save' button,  the button's width is adjusted, and the button is converted to a separator. 

void CMainFrame::AddCombo()
{
  // We'll be placing the ComboBoxEx control over the 'File Save' toolbar button
  int nComboWidth = 120;
  CToolBar& TB = GetToolBar();
  if (TB.CommandToIndex(IDM_FILE_SAVE) < 0) return;

  TB.SetButtonStyle(IDM_FILE_SAVE, TBSTYLE_SEP);	// Convert the button to a separator
  TB.SetButtonWidth(IDM_FILE_SAVE, nComboWidth);

  // Determine the size and position of the ComboBox
  int nIndex = TB.CommandToIndex(IDM_FILE_SAVE);
  CRect rc = TB.GetItemRect(nIndex);

  // Create and position the ComboboxEx window
  m_ComboBoxEx.Create(TB.GetHwnd());
  m_ComboBoxEx.SetWindowPos(NULL, rc, SWP_NOACTIVATE);

  // Set ComboBox Height
  m_ComboBoxEx.SendMessage(CB_SETITEMHEIGHT, (WPARAM)-1, (LPARAM)rc.Height()-6);

  m_ComboBoxEx.AddItems();
  RecalcLayout();
}

Refer to the remarks section in the documentation for CFrame for an illustration on how to define an configure the toolbar.

Summary Information

Header file toolbar.h
Win32/64 support Yes
WinCE support Yes
Library required Comctl32.lib