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. |
virtual int AddBitmap(UINT ToolBarID);Adds a bitmap to the toolbar. See also SetImages.
virtual BOOL AddButton(UINT nID, BOOL bEnabled = TRUE);Adds one button to the toolbar.
BOOL AddButtons(UINT uNumButtons, LPTBBUTTON lpButtons) const;Adds one or more buttons to the toolbar.
int AddString(UINT nStringID) const;Adds resource IDs to toolbar buttons.
int AddStrings(LPCTSTR lpszStrings) const;Adds strings to the toolbar's string pool.
void Autosize() const;Resizes the toolbar.
void CheckButton(int idButton, BOOL fCheck) const;Checks or unchecks a given button in a toolbar.
int CommandToIndex(int idButton) const;Returns the button index, given the command ID.
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.
BOOL DeleteButton(int iButton) const;Deletes a button from the toolbar
BOOL DisableButton(int idButton) const;Disables a toolbar button.
BOOL EnableButton(int idButton) const;Enables a toolbar button.
BOOL GetButton(int iButton, LPTBBUTTON lpButton) const;Retrieves information about the specified button in the toolbar.
int GetButtonCount() const;Returns the number of buttons in the toolbar control.
DWORD GetButtonSize() const;Retrieves the current width and height of toolbar buttons, in pixels.
UINT GetButtonState(int idButton) const;Returns the state of a toolbar button.
BYTE GetButtonStyle(int idButton) constReturns the style of toolbar button.
LPCTSTR GetButtonText(int idButton) const;Retrieves the display text of a button on the toolbar.
int GetCommandID(int iIndex) const;Returns the command ID given the button index.
HIMAGELIST GetDisabledImageList() const;Retrieves the image list that the toolbar uses to display inactive buttons.
HIMAGELIST GetImageList() const;Retrieves the image list that the toolbar uses to display hot buttons.
int GetHotItem() const;Retrieves the index of the hot button.
HIMAGELIST GetImageList() const;Retrieves the image list that the toolbar uses to display buttons in their default state.
CRect GetItemRect(int iIndex) const;Returns the bounding rectangle of a button in a toolbar.
CSize GetMaxSize() const;Returns the SIZE required to contain the toolbar's buttons.
DWORD GetPadding() const;Retrieves the padding for the toolbar.
CRect GetRect(int idButton) const;Retrieves the bounding rectangle for a specified toolbar button.
int GetRows() const;Retrieves the number of rows of buttons in the toolbar.
int GetTextRows() const;Retrieves the maximum number of text rows that can be displayed on a toolbar button.
std::vector<UINT>& GetToolBarData() constReturns a reference to the vector of resource IDs for toolbar buttons.
CToolTip* GetToolTips() const;Retrieves the handle to the TooTip control, if any, associated with the toolbar.
BOOL HasText() const;Returns TRUE if the button has text.
BOOL HideButton(int idButton, BOOL fShow) const;Hides or shows the specified toolbar button.
int HitTest() const;Determines which button a point lies in a toolbar control.
BOOL Indeterminate(int idButton, BOOL fIndeterminate) const;Sets or clears the indeterminate state of the specified toolbar button.
BOOL InsetButton(int iButton, LPTBBUTTON lpButton) const;Inserts a button in the toolbar.
BOOL IsButtonHidden(int idButton) const;Determines whether the specified toolbar button is hidden.
BOOL IsButtonHighlighted(int idButton) const;Checks the highlight state of the toolbar button.
BOOL IsButtonIndeterminate(int idButton) const;Determines whether the specified toolbar button is indeterminate.
BOOL IsButtonPressed(int idButton) const;Determines whether the specified toolbar button is pressed.
int MapAccelerator(TCHAR chAccel) const;Determines the ID of the button that corresponds to the specified accelerator character.
BOOL MarkButton(int idButton) const;Sets the highlight state of a given button in a toolbar control.
BOOL MoveButton(UINT uOldPos, UINT uNewPos) const;Moves a button from one index to another.
BOOL PressButton(int idButton, BOOL fPress) const;Presses or releases the specified toolbar button.
virtual BOOL ReplaceBitmap(UINT NewToolBarID);Replaces a toolbar's bitmap. See also SetImages.
void SaveRestore(BOOL fSave, TBSAVEPARAMS* ptbsp) const;Initiates saving or restoring the toolbar's state.
virtual BOOL SetBitmap(UINT nID);Sets a toolbar's bitmap. See also SetImages.
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.
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.
BOOL SetBitmapSize(int cx, int cy) const;Sets the size of the buttons to be added to a toolbar.
BOOL SetButtonState(int idButton, UINT State) const;Sets the state of a toolbar button.
BOOL SetButtonStyle(int idButton, BYTE Style) const;Sets the style of a toolbar button.
virtual BOOL SetButtonText(int idButton, LPCTSTR szText);Sets the text for a toolbar button.
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.
BOOL SetCommandID(int iIndex, int idButton) const;Sets the Command ID of a toolbar button.
HIMAGELIST SetDisableImageList(HIMAGELIST himlNewDisabled) const;Sets the image list that the toolbar will use to display disabled buttons.
DWORD SetDrawTextFlags(DWORD dwMask, DWORD dwDTFlags) const;Sets the text drawing flags for the toolbar.
DWORD SetExtendedStyle(DWORD dwExStyle) const;Sets the extended styles for the toolbar.
HIMAGELIST SetHotImageList(HIMAGELIST himlNewHot) const;Sets the image list that the toolbar will use to display hot buttons.
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.
HIMAGELIST SetImageList(HIMAGELIST himlNew) const;Sets the image list that the toolbar will use to display buttons that are in their default state.
BOOL SetIndent(int iIndent) const;Sets the indentation for the first toolbar button.
BOOL SetMaxTextRows(int iMaxRows) const;Sets the maximum number of text rows displayed on a toolbar button.
BOOL SetPadding(int cx, int cy) const;Sets the padding for the toolbar.
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 |