CToolBar 类CToolBar Class

具有一行位图化按钮和可选分隔符的控件条。Control bars that have a row of bitmapped buttons and optional separators.

语法Syntax

class CToolBar : public CControlBar

成员Members

公共构造函数Public Constructors

名称Name 说明Description
CToolBar:CToolBarCToolBar::CToolBar 构造 CToolBar 对象。Constructs a CToolBar object.

公共方法Public Methods

名称Name 说明Description
CToolbar::命令索引CToolBar::CommandToIndex 返回具有给定命令 ID 的按钮的索引。Returns the index of a button with the given command ID.
CToolBar::创建CToolBar::Create 创建 Windows 工具栏并将其附加到CToolBar对象。Creates the Windows toolbar and attaches it to the CToolBar object.
CToolBar::创建ExCToolBar::CreateEx CToolBar嵌入CToolBarCtrl对象创建具有其他样式的对象。Creates a CToolBar object with additional styles for the embedded CToolBarCtrl object.
CToolBar:获取按钮信息CToolBar::GetButtonInfo 检索按钮的 ID、样式和图像编号。Retrieves the ID, style, and image number of a button.
CToolBar:获取按钮样式CToolBar::GetButtonStyle 检索按钮的样式。Retrieves the style for a button.
CToolBar:获取按钮文本CToolBar::GetButtonText 检索将显示在按钮上的文本。Retrieves the text that will appear on a button.
CToolBar:获取项目IDCToolBar::GetItemID 在给定索引处返回按钮或分隔符的命令 ID。Returns the command ID of a button or separator at the given index.
CToolBar:获取项目重新完成CToolBar::GetItemRect 检索给定索引处项的显示矩形。Retrieves the display rectangle for the item at the given index.
CToolBar::获取工具栏CToolBar::GetToolBarCtrl 允许直接访问基础公共控件。Allows direct access to the underlying common control.
CToolBar::加载位图CToolBar::LoadBitmap 加载包含位图按钮图像的位图。Loads the bitmap containing bitmap-button images.
CToolBar::加载工具栏CToolBar::LoadToolBar 加载使用资源编辑器创建的工具栏资源。Loads a toolbar resource created with the resource editor.
CToolBar::设置位图CToolBar::SetBitmap 设置位映射图像。Sets a bitmapped image.
CToolBar::设置按钮信息CToolBar::SetButtonInfo 设置按钮的 ID、样式和图像编号。Sets the ID, style, and image number of a button.
CToolBar:设置按钮CToolBar::SetButtons 设置位图中的按钮样式和按钮图像索引。Sets button styles and an index of button images within the bitmap.
CToolBar:设置按钮样式CToolBar::SetButtonStyle 设置按钮的样式。Sets the style for a button.
CToolBar::设置按钮文本CToolBar::SetButtonText 设置将显示在按钮上的文本。Sets the text that will appear on a button.
CToolBar::设置高度CToolBar::SetHeight 设置工具栏的高度。Sets the height of the toolbar.
CToolBar::设置大小CToolBar::SetSizes 设置按钮的大小及其位图。Sets the sizes of buttons and their bitmaps.

备注Remarks

按钮可以像按钮、复选框按钮或单选按钮一样。The buttons can act like pushbuttons, check-box buttons, or radio buttons. CToolBar对象通常是派生自类CFrameWndCMDIFrameWnd的帧窗口对象的嵌入成员。CToolBar objects are usually embedded members of frame-window objects derived from the class CFrameWnd or CMDIFrameWnd.

CToolBar:GetToolBarCtrl是 MFC 4.0 中新成员函数,允许您利用 Windows 通用控件对工具栏自定义和其他功能的支持。CToolBar::GetToolBarCtrl, a member function new to MFC 4.0, allows you to take advantage of the Windows common control's support for toolbar customization and additional functionality. CToolBar成员函数为您提供 Windows 公共控件的大部分功能;但是,当您调用GetToolBarCtrl时,您可以为您的工具栏提供更多 Windows 95/98 工具栏的特征。CToolBar member functions give you most of the functionality of the Windows common controls; however, when you call GetToolBarCtrl, you can give your toolbars even more of the characteristics of Windows 95/98 toolbars. 调用GetToolBarCtrl时,它将返回对CToolBarCtrl对象的引用。When you call GetToolBarCtrl, it will return a reference to a CToolBarCtrl object. 有关使用 Windows 常见控件设计工具栏的详细信息,请参阅CToolBarCtrl。See CToolBarCtrl for more information about designing toolbars using Windows common controls. 有关常见控件的更多一般信息,请参阅 Windows SDK 中的常见控件For more general information about common controls, see Common Controls in the Windows SDK.

可视化C++为您提供了创建工具栏的两种方法。Visual C++ provides you with two methods to create a toolbar. 要使用资源编辑器创建工具栏资源,请按照以下步骤操作:To create a toolbar resource using the Resource Editor, follow these steps:

  1. 创建工具栏资源。Create a toolbar resource.

  2. 构造 CToolBar 对象。Construct the CToolBar object.

  3. 调用"创建"(或CreateEx) 函数以创建 Windows 工具栏并将其附加到CToolBar对象。Call the Create (or CreateEx) function to create the Windows toolbar and attach it to the CToolBar object.

  4. 调用LoadToolBar以加载工具栏资源。Call LoadToolBar to load the toolbar resource.

否则,请执行以下步骤:Otherwise, follow these steps:

  1. 构造 CToolBar 对象。Construct the CToolBar object.

  2. 调用"创建"(或CreateEx) 函数以创建 Windows 工具栏并将其附加到CToolBar对象。Call the Create (or CreateEx) function to create the Windows toolbar and attach it to the CToolBar object.

  3. 调用LoadBitmap以加载包含工具栏按钮图像的位图。Call LoadBitmap to load the bitmap that contains the toolbar button images.

  4. 调用SetButtons以设置按钮样式,并将每个按钮与位图中的图像相关联。Call SetButtons to set the button style and associate each button with an image in the bitmap.

工具栏中的所有按钮图像都取自一个位图,该位图必须为每个按钮包含一个图像。All the button images in the toolbar are taken from one bitmap, which must contain one image for each button. 所有图像的大小必须相同;默认值为 16 像素宽,高 15 像素。All images must be the same size; the default is 16 pixels wide and 15 pixels high. 图像必须并排在位图中。Images must be side by side in the bitmap.

函数SetButtons获取指向控件指示数组的指针和指定数组中元素数的整数。The SetButtons function takes a pointer to an array of control IDs and an integer that specifies the number of elements in the array. 该函数将每个按钮的 ID 设置到数组的相应元素的值,并为每个按钮分配一个图像索引,该索引指定按钮图像在位图中的位置。The function sets each button's ID to the value of the corresponding element of the array and assigns each button an image index, which specifies the position of the button's image in the bitmap. 如果数组元素的值ID_SEPARATOR,则不分配图像索引。If an array element has the value ID_SEPARATOR, no image index is assigned.

位图中图像的顺序通常是在屏幕上绘制图像的顺序,但您可以使用SetButtonInfo函数来更改图像顺序和绘图顺序之间的关系。The order of the images in the bitmap is typically the order in which they are drawn on the screen, but you can use the SetButtonInfo function to change the relationship between image order and drawing order.

工具栏中的所有按钮大小都相同。All buttons in a toolbar are the same size. 默认值为 24 x 22 像素,根据Windows 界面软件设计指南The default is 24 x 22 pixels, in accordance with Windows Interface Guidelines for Software Design. 图像和按钮尺寸之间的任何附加空间都用于在图像周围形成边框。Any additional space between the image and button dimensions is used to form a border around the image.

每个按钮都有一个图像。Each button has one image. 各种按钮状态和样式(按下、向上、向下、禁用、向下禁用和不确定)从该图像生成。The various button states and styles (pressed, up, down, disabled, disabled down, and indeterminate) are generated from that one image. 尽管位图可以是任何颜色,但您可以使用黑色和灰色阴影的图像获得最佳效果。Although bitmaps can be any color, you can achieve the best results with images in black and shades of gray.

警告

CToolBar支持最多 16 种颜色的位图。CToolBar supports bitmaps with a maximum of 16 colors. 将图像加载到工具栏编辑器中时,Visual Studio 会自动将图像转换为 16 色位图(如有必要),如果图像已转换,则显示警告消息。When you load an image into a toolbar editor, Visual Studio automatically converts the image to a 16-color bitmap, if necessary, and displays a warning message if the image was converted. 如果使用具有 16 种颜色以上的图像(使用外部编辑器编辑图像),则应用程序可能会出乎意料地运行。If you use an image with more than 16 colors (using an external editor to edit the image), the application might behave unexpectedly.

默认情况下,工具栏按钮会模仿按钮。Toolbar buttons imitate pushbuttons by default. 但是,工具栏按钮也可以模仿复选框按钮或单选按钮。However, toolbar buttons can also imitate check-box buttons or radio buttons. 复选框按钮有三种状态:已选中、清除和不确定。Check-box buttons have three states: checked, cleared, and indeterminate. 单选按钮只有两种状态:已选中并清除。Radio buttons have only two states: checked and cleared.

若要在不指向数组的情况下设置单个按钮或分隔符样式,请调用GetButtonStyle来检索该样式,然后调用SetButtonStyle而不是SetButtonsTo set an individual button or separator style without pointing to an array, call GetButtonStyle to retrieve the style, and then call SetButtonStyle instead of SetButtons. SetButtonStyle当您想在运行时更改按钮的样式时,最有用。SetButtonStyle is most useful when you want to change a button's style at run time.

要将文本指定在按钮上显示,请致电GetButtonText以检索显示在按钮上的文本,然后调用SetButtonText来设置文本。To assign text to appear on a button, call GetButtonText to retrieve the text to appear on the button, and then call SetButtonText to set the text.

要创建复选框按钮,请为其分配样式TBBS_CHECKBOX或在ON_UPDATE_COMMAND_UI处理程序中使用CCmdUI对象SetCheck的成员函数。To create a check-box button, assign it the style TBBS_CHECKBOX or use a CCmdUI object's SetCheck member function in an ON_UPDATE_COMMAND_UI handler. 呼叫SetCheck将按钮转换为复选框按钮。Calling SetCheck turns a pushbutton into a check-box button. SetCheck参数 0 表示未选中,1 表示选中,2 表示不确定。Pass SetCheck an argument of 0 for unchecked, 1 for checked, or 2 for indeterminate.

要创建单选按钮,请从ON_UPDATE_COMMAND_UI处理程序调用CCmdUI对象的SetRadio成员函数。To create a radio button, call a CCmdUI object's SetRadio member function from an ON_UPDATE_COMMAND_UI handler. SetRadio未选中或未选中传递参数 0 或非零。Pass SetRadio an argument of 0 for unchecked or nonzero for checked. 为了提供无线电组的互斥行为,您必须为组中的所有按钮ON_UPDATE_COMMAND_UI处理程序。In order to provide a radio group's mutually exclusive behavior, you must have ON_UPDATE_COMMAND_UI handlers for all of the buttons in the group.

有关使用CToolBar的详细信息,请参阅文章MFC 工具栏实现和技术说明 31:控制栏For more information on using CToolBar, see the article MFC Toolbar Implementation and Technical Note 31: Control Bars.

继承层次结构Inheritance Hierarchy

CObjectCObject

CCmdTargetCCmdTarget

CWndCWnd

C控制栏CControlBar

CToolBar

要求Requirements

标题: afxext.hHeader: afxext.h

CToolbar::命令索引CToolBar::CommandToIndex

此成员函数返回第一个工具栏按钮的索引,从命令 ID 匹配nIDFind的位置 0 开始。This member function returns the index of the first toolbar button, starting at position 0, whose command ID matches nIDFind.

int CommandToIndex(UINT nIDFind) const;

参数Parameters

nIDFindnIDFind
工具栏按钮的命令 ID。Command ID of a toolbar button.

返回值Return Value

按钮的索引,如果没有按钮具有给定的命令 ID,则为 -1。The index of the button, or -1 if no button has the given command ID.

CToolBar::创建CToolBar::Create

此成员函数创建 Windows 工具栏(子窗口),并将其与CToolBar对象关联。This member function creates a Windows toolbar (a child window) and associates it with the CToolBar object.

virtual BOOL Create(
    CWnd* pParentWnd,
    DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_TOP,
    UINT nID = AFX_IDW_TOOLBAR);

参数Parameters

pparentwndpParentWnd
指向工具栏的父窗口。Pointer to the window that is the toolbar's parent.

dwStyledwStyle
工具栏样式。The toolbar style. 支持的其他工具栏样式包括:Additional toolbar styles supported are:

  • CBRS_TOP控制栏位于框架窗口的顶部。CBRS_TOP Control bar is at top of the frame window.

  • CBRS_BOTTOM控制栏位于框架窗口的底部。CBRS_BOTTOM Control bar is at bottom of the frame window.

  • CBRS_NOALIGN调整父控件栏的大小时,不会重新定位。CBRS_NOALIGN Control bar is not repositioned when the parent is resized.

  • CBRS_TOOLTIPS控制栏显示工具提示。CBRS_TOOLTIPS Control bar displays tool tips.

  • CBRS_SIZE_DYNAMIC控制栏是动态的。CBRS_SIZE_DYNAMIC Control bar is dynamic.

  • CBRS_SIZE_FIXED控制栏已固定。CBRS_SIZE_FIXED Control bar is fixed.

  • CBRS_FLOATING控制栏是浮动的。CBRS_FLOATING Control bar is floating.

  • CBRS_FLYBY 状态栏显示有关按钮的信息。CBRS_FLYBY Status bar displays information about the button.

  • CBRS_HIDE_INPLACE控件栏不向用户显示。CBRS_HIDE_INPLACE Control bar is not displayed to the user.

nIDnID
工具栏的子窗口 ID。The toolbar's child-window ID.

返回值Return Value

如果成功,则不为 0;否则为 0。Nonzero if successful; otherwise 0.

备注Remarks

它还将工具栏高度设置为默认值。It also sets the toolbar height to a default value.

示例Example

// This code fragment is taken from CMainFrame::OnCreate
// CMainFrame is derived from CMDIFrameWnd.

//This example creates a dockable toolbar.
if (!m_wndToolBar.Create(this) ||
   !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
   TRACE0("Failed to create toolbar\n");
   return -1;      // fail to create
}

//Make the toolbar dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);

CToolBar::创建ExCToolBar::CreateEx

调用此函数以创建 Windows 工具栏(子窗口),并将其与CToolBar对象关联。Call this function to create a Windows toolbar (a child window) and associate it with the CToolBar object.

virtual BOOL CreateEx(
    CWnd* pParentWnd,
    DWORD dwCtrlStyle = TBSTYLE_FLAT,
    DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP,
    CRect rcBorders = CRect(
    0,
    0,
    0,
    0),
    UINT nID = AFX_IDW_TOOLBAR);

参数Parameters

pparentwndpParentWnd
指向工具栏的父窗口。Pointer to the window that is the toolbar's parent.

dwCtrl风格dwCtrlStyle
用于创建嵌入式CToolBarCtrl对象的其他样式。Additional styles for the creation of the embedded CToolBarCtrl object. 默认情况下,此值设置为TBSTYLE_FLAT。By default, this value is set to TBSTYLE_FLAT. 有关工具栏样式的完整列表,请参阅dwStyleFor a complete list of toolbar styles, see dwStyle.

dwStyledwStyle
工具栏样式。The toolbar style. 有关适当样式的列表,请参阅 Windows SDK 中的工具栏控件和按钮样式See Toolbar Control and Button Styles in the Windows SDK for a list of appropriate styles.

rcBordersrcBorders
定义工具栏窗口边框宽度的CRect对象。A CRect object that defines the widths of the toolbar window borders. 默认情况下,这些边框设置为 0,0,0,0,从而生成没有边框的工具栏窗口。These borders are set to 0,0,0,0 by default, thereby resulting in a toolbar window with no borders.

nIDnID
工具栏的子窗口 ID。The toolbar's child-window ID.

返回值Return Value

如果成功,则不为 0;否则为 0。Nonzero if successful; otherwise 0.

备注Remarks

它还将工具栏高度设置为默认值。It also sets the toolbar height to a default value.

CreateEx创建嵌入工具栏控件期间需要存在某些样式时,请使用 而不是Create。Use CreateEx, instead of Create, when certain styles need to be present during the creation of the embedded tool bar control. 例如,将dwCtrlStyle设置为TBSTYLE_FLAT|TBSTYLE_TRANSPARENT创建类似于 Internet Explorer 4 工具栏的工具栏。For example, set dwCtrlStyle to TBSTYLE_FLAT | TBSTYLE_TRANSPARENT to create a toolbar that resembles the Internet Explorer 4 toolbars.

示例Example

// This example demonstrates CToolBar::CreateEx by creating a 
// toolbar as part of a child frame window. It also calls the 
// LoadToolbar and EnableDocking functions
int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
   if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
      return -1;

   if (!m_wndMyToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
      | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
      !m_wndMyToolBar.LoadToolBar(IDR_MYTOOLBAR))
   {
      TRACE0("Failed to create toolbar\n");
      return -1;      // fail to create
   }

   m_wndMyToolBar.EnableDocking(CBRS_ALIGN_ANY);
   EnableDocking(CBRS_ALIGN_ANY);
   DockControlBar(&m_wndMyToolBar);

   return 0;
}

CToolBar:CToolBarCToolBar::CToolBar

此成员函数构造对象CToolBar并设置默认大小。This member function constructs a CToolBar object and sets the default sizes.

CToolBar();

备注Remarks

调用"创建成员"函数以创建工具栏窗口。Call the Create member function to create the toolbar window.

CToolBar:获取按钮信息CToolBar::GetButtonInfo

此成员函数检索 nIndex 指定位置工具栏按钮或分隔符的控制 ID、样式和图像索引。This member function retrieves the control ID, style, and image index of the toolbar button or separator at the location specified by nIndex.

void GetButtonInfo(
    int nIndex,
    UINT& nID,
    UINT& nStyle,
    int& iImage) const;

参数Parameters

nIndexnIndex
要检索其信息的工具栏按钮或分隔符的索引。Index of the toolbar button or separator whose information is to be retrieved.

nIDnID
对设置为按钮的命令 ID 的 UINT 的引用。Reference to a UINT that is set to the command ID of the button.

nStylenStyle
引用设置为按钮样式的 UINT。Reference to a UINT that is set to the style of the button.

i图像iImage
引用设置为位图中按钮图像索引的整数。Reference to an integer that is set to the index of the button's image within the bitmap.

备注Remarks

这些值分配给nID、nStyle**nIDiImage引用的变量。Those values are assigned to the variables referenced by nID, nStyle, and iImage. 图像索引是图像在位图中的位置,该位图包含所有工具栏按钮的图像。The image index is the position of the image within the bitmap that contains images for all the toolbar buttons. 第一个图像位于位置 0。The first image is at position 0.

如果nIndex指定分隔符,则iImage设置为分隔符宽度(以像素为单位)。If nIndex specifies a separator, iImage is set to the separator width in pixels.

CToolBar:获取按钮样式CToolBar::GetButtonStyle

调用此成员函数以检索工具栏上的按钮或分隔符的样式。Call this member function to retrieve the style of a button or separator on the toolbar.

UINT GetButtonStyle(int nIndex) const;

参数Parameters

nIndexnIndex
要检索的工具栏按钮或分隔符样式的索引。The index of the toolbar button or separator style to be retrieved.

返回值Return Value

nIndex指定的按钮或分隔符的样式。The style of the button or separator specified by nIndex.

备注Remarks

按钮的样式确定按钮的显示方式以及按钮如何响应用户输入。A button's style determines how the button appears and how it responds to user input. 有关按钮样式的示例,请参阅设置ButtonStyle。See SetButtonStyle for examples of button styles.

CToolBar:获取按钮文本CToolBar::GetButtonText

调用此成员函数以检索显示在按钮上的文本。Call this member function to retrieve the text that appears on a button.

CString GetButtonText(int nIndex) const;

void GetButtonText(
    int nIndex,
    CString& rString) const;

参数Parameters

nIndexnIndex
要检索的文本的索引。Index of the text to be retrieved.

rStringrString
对包含要检索的文本的CString对象的引用。A reference to a CString object that will contain the text to be retrieved.

返回值Return Value

包含CString按钮文本的对象。A CString object containing the button text.

备注Remarks

此成员函数的第二种形式使用字符串文本填充CString对象。The second form of this member function fills a CString object with the string text.

CToolBar:获取项目IDCToolBar::GetItemID

此成员函数返回nIndex指定的按钮或分隔符的命令 ID。This member function returns the command ID of the button or separator specified by nIndex.

UINT GetItemID(int nIndex) const;

参数Parameters

nIndexnIndex
要检索其 ID 的项的索引。Index of the item whose ID is to be retrieved.

返回值Return Value

nIndex指定的按钮或分隔符的命令 ID。The command ID of the button or separator specified by nIndex.

备注Remarks

分离器返回ID_SEPARATOR。Separators return ID_SEPARATOR.

CToolBar:获取项目重新完成CToolBar::GetItemRect

此成员函数使用RECTnIndex指定的按钮或分隔符的坐标填充其地址包含在lpRect中的结构。This member function fills the RECT structure whose address is contained in lpRect with the coordinates of the button or separator specified by nIndex.

virtual void GetItemRect(
    int nIndex,
    LPRECT lpRect) const;

参数Parameters

nIndexnIndex
要检索其矩形坐标的项的索引(按钮或分隔符)。Index of the item (button or separator) whose rectangle coordinates are to be retrieved.

lpRectlpRect
将包含项坐标的RECT结构的地址。Address of the RECT structure that will contain the item's coordinates.

备注Remarks

坐标相对于工具栏的左上角以像素为单位。Coordinates are in pixels relative to the upper-left corner of the toolbar.

用于GetItemRect获取要替换为组合框或其他控件的分隔符的坐标。Use GetItemRect to get the coordinates of a separator you want to replace with a combo box or other control.

示例Example

请参阅CToolBar 的示例:设置大小See the example for CToolBar::SetSizes.

CToolBar::获取工具栏CToolBar::GetToolBarCtrl

此成员函数允许直接访问基础公共控件。This member function allows direct access to the underlying common control.

CToolBarCtrl& GetToolBarCtrl() const;

返回值Return Value

CToolBarCtrl 对象的引用。A reference to a CToolBarCtrl object.

备注Remarks

用于GetToolBarCtrl利用 Windows 工具栏通用控件的功能,并利用CToolBarCtrl提供的工具栏自定义支持。Use GetToolBarCtrl to take advantage of the functionality of the Windows toolbar common control, and to take advantage of the support CToolBarCtrl provides for toolbar customization.

有关使用常见控件的详细信息,请参阅 Windows SDK 中的"控件"和"常见控件"一文。For more information about using common controls, see the article Controls and Common Controls in the Windows SDK.

示例Example

// This code fragment is taken from CMainFrame::OnCreate
// CMainFrame is derived from CMDIFrameWnd.

//This example shows how to add text to toolbar buttons.
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE
   | CBRS_TOP) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
   TRACE0("Failed to create toolbar\n");
   return -1;      // fail to create
}

//Show text on toolbar buttons.
VERIFY(m_wndToolBar.SetButtonText(0, _T("New")));
VERIFY(m_wndToolBar.SetButtonText(1, _T("Open")));
VERIFY(m_wndToolBar.SetButtonText(2, _T("Save")));
VERIFY(m_wndToolBar.SetButtonText(4, _T("Cut")));
VERIFY(m_wndToolBar.SetButtonText(5, _T("Copy")));
VERIFY(m_wndToolBar.SetButtonText(6, _T("Paste")));
VERIFY(m_wndToolBar.SetButtonText(8, _T("Print")));
VERIFY(m_wndToolBar.SetButtonText(9, _T("About")));

CRect temp;
m_wndToolBar.GetItemRect(0, &temp);
m_wndToolBar.GetToolBarCtrl().SetButtonSize(CSize(temp.Width(),
   temp.Height()));

CToolBar::加载位图CToolBar::LoadBitmap

调用此成员函数以加载 或lpszResourceName``nIDResource指定的位图。Call this member function to load the bitmap specified by lpszResourceName or nIDResource.

BOOL LoadBitmap(LPCTSTR lpszResourceName);
BOOL LoadBitmap(UINT nIDResource);

参数Parameters

lpsz 资源名称lpszResourceName
指向要加载的位图的资源名称的指针。Pointer to the resource name of the bitmap to be loaded.

nID资源nIDResource
要加载的位图的资源 ID。Resource ID of the bitmap to be loaded.

返回值Return Value

如果成功,则不为 0;否则为 0。Nonzero if successful; otherwise 0.

备注Remarks

位图应包含每个工具栏按钮的一个图像。The bitmap should contain one image for each toolbar button. 如果图像不是标准大小(16 像素宽,15 像素高),请调用SetSizes来设置按钮大小及其图像。If the images are not of the standard size (16 pixels wide and 15 pixels high), call SetSizes to set the button sizes and their images.

警告

CToolBar支持最多 16 种颜色的位图。CToolBar supports bitmaps with a maximum of 16 colors. 将图像加载到工具栏编辑器中时,Visual Studio 会自动将图像转换为 16 色位图(如有必要),如果图像已转换,则显示警告消息。When you load an image into a toolbar editor, Visual Studio automatically converts the image to a 16-color bitmap, if necessary, and displays a warning message if the image was converted. 如果使用具有 16 种颜色以上的图像(使用外部编辑器编辑图像),则应用程序可能会出乎意料地运行。If you use an image with more than 16 colors (using an external editor to edit the image), the application might behave unexpectedly.

CToolBar::加载工具栏CToolBar::LoadToolBar

调用此成员函数以加载lpszResourceNamenIDResource指定的工具栏。Call this member function to load the toolbar specified by lpszResourceName or nIDResource.

BOOL LoadToolBar(LPCTSTR lpszResourceName);
BOOL LoadToolBar(UINT nIDResource);

参数Parameters

lpsz 资源名称lpszResourceName
指向要加载的工具栏的资源名称的指针。Pointer to the resource name of the toolbar to be loaded.

nID资源nIDResource
要加载的工具栏的资源 ID。Resource ID of the toolbar to be loaded.

返回值Return Value

如果成功,则不为 0;否则为 0。Nonzero if successful; otherwise 0.

备注Remarks

有关创建工具栏资源的详细信息,请参阅 中的工具栏编辑器See toolbar editor in for more information about creating a toolbar resource.

示例Example

请参阅CToolBar 的示例::创建ExSee the example for CToolBar::CreateEx.

CToolBar::设置位图CToolBar::SetBitmap

调用此成员函数以设置工具栏的位图图像。Call this member function to set the bitmap image for the toolbar.

BOOL SetBitmap(HBITMAP hbmImageWell);

参数Parameters

hbmImageWellhbmImageWell
与工具栏关联的位图图像的句柄。Handle of a bitmap image that is associated with a toolbar.

返回值Return Value

如果成功,则不为 0;否则为 0。Nonzero if successful; otherwise 0.

备注Remarks

例如,在用户SetBitmap对更改按钮操作的文档执行操作后,调用更改位映射图像。For example, call SetBitmap to change the bitmapped image after the user takes an action on a document that changes the action of a button.

CToolBar::设置按钮信息CToolBar::SetButtonInfo

调用此成员函数以设置按钮的命令 ID、样式和图像编号。Call this member function to set the button's command ID, style, and image number.

void SetButtonInfo(
    int nIndex,
    UINT nID,
    UINT nStyle,
    int iImage);

参数Parameters

nIndexnIndex
要为其设置信息的按钮或分隔符的零基索引。Zero-based index of the button or separator for which information is to be set.

nIDnID
按钮的命令 ID 设置为的值。The value to which the button's command ID is set.

nStylenStyle
新的按钮样式。The new button style. 支持以下按钮样式:The following button styles are supported:

  • TBBS_BUTTON标准按钮(默认)TBBS_BUTTON Standard pushbutton (default)

  • TBBS_SEPARATOR分离器TBBS_SEPARATOR Separator

  • TBBS_CHECKBOX 自动复选框按钮TBBS_CHECKBOX Auto check-box button

  • TBBS_GROUP 标记一组按钮的开始TBBS_GROUP Marks the start of a group of buttons

  • TBBS_CHECKGROUP 标记一组复选框按钮的开始TBBS_CHECKGROUP Marks the start of a group of check-box buttons

  • TBBS_DROPDOWN 创建下拉列表按钮。TBBS_DROPDOWN Creates a drop-down list button.

  • TBBS_AUTOSIZE 按钮的宽度将根据按钮的文本而不是图像的大小进行计算。TBBS_AUTOSIZE The button's width will be calculated based on the text of the button, not on the size of the image.

  • TBBS_NOPREFIX 按钮文本将没有与其关联的快捷键前缀。TBBS_NOPREFIX The button text will not have an accelerator prefix associated with it.

i图像iImage
位图中按钮图像的新索引。New index for the button's image within the bitmap.

备注Remarks

对于具有样式TBBS_SEPARATOR的分隔符,此函数将分隔符的宽度(以像素为单位)设置为存储在iImage中的值。For separators, which have the style TBBS_SEPARATOR, this function sets the separator's width in pixels to the value stored in iImage.

备注

您还可以使用nStyle参数设置按钮状态;但是,由于按钮状态由ON_UPDATE_COMMAND_UI处理程序控制,因此在下次空闲处理期间,SetButtonInfo您设置使用的任何状态都将丢失。You can also set button states using the nStyle parameter; however, because button states are controlled by the ON_UPDATE_COMMAND_UI handler, any state you set using SetButtonInfo will be lost during the next idle processing. 有关详细信息,请参阅如何更新用户界面对象TN031:控制栏See How to Update User-Interface Objects and TN031: Control Bars for more information.

有关位图图像和按钮的信息,请参阅CToolBar概述和CToolBar::LoadBitmapFor information on bitmap images and buttons, see the CToolBar Overview and CToolBar::LoadBitmap.

CToolBar:设置按钮CToolBar::SetButtons

此成员函数将每个工具栏按钮的命令 ID 设置到数组lpIDArray的相应元素指定的值。This member function sets each toolbar button's command ID to the value specified by the corresponding element of the array lpIDArray.

BOOL SetButtons(
    const UINT* lpIDArray,
    int nIDCount);

参数Parameters

lpIDArraylpIDArray
指向命令 ID 的数组的指针。Pointer to an array of command Ids. 分配空按钮可以为 NULL。It can be NULL to allocate empty buttons.

nIDCountnIDCount
lpIDArray指向的数组中的元素数。Number of elements in the array pointed to by lpIDArray.

返回值Return Value

如果成功,则不为 0;否则为 0。Nonzero if successful; otherwise 0.

备注Remarks

如果数组的元素具有值ID_SEPARATOR,则在工具栏的相应位置创建分隔符。If an element of the array has the value ID_SEPARATOR, a separator is created in the corresponding position of the toolbar. 此函数还将每个按钮的样式设置为TBBS_BUTTON,每个分隔符的样式TBBS_SEPARATOR,并将图像索引分配给每个按钮。This function also sets each button's style to TBBS_BUTTON and each separator's style to TBBS_SEPARATOR, and assigns an image index to each button. 图像索引指定按钮图像在位图中的位置。The image index specifies the position of the button's image within the bitmap.

不需要考虑位图中的分隔符,因为此函数不为分隔符分配图像索引。You do not need to account for separators in the bitmap because this function does not assign image indexes for separators. 如果工具栏在位置 0、1 和 3 处具有按钮,在位置 2 处有分隔符,则位图中位置 0、1 和 2 处的图像将分别分配给位置 0、1 和 3 处的按钮。If your toolbar has buttons at positions 0, 1, and 3 and a separator at position 2, the images at positions 0, 1, and 2 in your bitmap are assigned to the buttons at positions 0, 1, and 3, respectively.

如果lpIDArray为 NULL,则此函数为nIDCount指定的项数分配空间。If lpIDArray is NULL, this function allocates space for the number of items specified by nIDCount. 使用SetButtonInfo设置每个项目的属性。Use SetButtonInfo to set each item's attributes.

CToolBar:设置按钮样式CToolBar::SetButtonStyle

调用此成员函数以设置按钮或分隔符的样式,或对按钮进行分组。Call this member function to set the style of a button or separator, or to group buttons.

void SetButtonStyle(
    int nIndex,
    UINT nStyle);

参数Parameters

nIndexnIndex
要设置其信息的按钮或分隔符的索引。Index of the button or separator whose information is to be set.

nStylenStyle
按钮样式。The button style. 支持以下按钮样式:The following button styles are supported:

  • TBBS_BUTTON标准按钮(默认)TBBS_BUTTON Standard pushbutton (default)

  • TBBS_SEPARATOR分离器TBBS_SEPARATOR Separator

  • TBBS_CHECKBOX 自动复选框按钮TBBS_CHECKBOX Auto check-box button

  • TBBS_GROUP 标记一组按钮的开始TBBS_GROUP Marks the start of a group of buttons

  • TBBS_CHECKGROUP 标记一组复选框按钮的开始TBBS_CHECKGROUP Marks the start of a group of check-box buttons

  • TBBS_DROPDOWN 创建下拉列表按钮TBBS_DROPDOWN Creates a drop-down list button

  • TBBS_AUTOSIZE按钮的宽度将根据按钮的文本(而不是图像的大小)计算TBBS_AUTOSIZE The button's width will be calculated based on the text of the button, not on the size of the image

  • TBBS_NOPREFIX 按钮文本将没有与其关联的快捷键前缀TBBS_NOPREFIX The button text will not have an accelerator prefix associated with it

备注Remarks

按钮的样式确定按钮的显示方式以及按钮如何响应用户输入。A button's style determines how the button appears and how it responds to user input.

在调用SetButtonStyle之前,调用GetButtonStyle成员函数来检索按钮或分隔符样式。Before calling SetButtonStyle, call the GetButtonStyle member function to retrieve the button or separator style.

备注

您还可以使用nStyle参数设置按钮状态;但是,由于按钮状态由ON_UPDATE_COMMAND_UI处理程序控制,因此在下次空闲处理期间,SetButtonStyle您设置使用的任何状态都将丢失。You can also set button states using the nStyle parameter; however, because button states are controlled by the ON_UPDATE_COMMAND_UI handler, any state you set using SetButtonStyle will be lost during the next idle processing. 有关详细信息,请参阅如何更新用户界面对象TN031:控制栏See How to Update User-Interface Objects and TN031: Control Bars for more information.

CToolBar::设置按钮文本CToolBar::SetButtonText

调用此函数以设置按钮上的文本。Call this function to set the text on a button.

BOOL SetButtonText(
    int nIndex,
    LPCTSTR lpszText);

参数Parameters

nIndexnIndex
要设置其文本的按钮的索引。Index of the button whose text is to be set.

lpszTextlpszText
指向要在按钮上设置的文本。Points to the text to be set on a button.

返回值Return Value

如果成功,则不为 0;否则为 0。Nonzero if successful; otherwise 0.

示例Example

请参阅CToolBar 的示例:获取工具栏。See the example for CToolBar::GetToolBarCtrl.

CToolBar::设置高度CToolBar::SetHeight

此成员函数将工具栏的高度设置为在 cyHeight中指定的值(以像素为单位)。This member function sets the toolbar's height to the value, in pixels, specified in cyHeight.

void SetHeight(int cyHeight);

参数Parameters

赛高地cyHeight
工具栏的高度(以像素为单位)。The height in pixels of the toolbar.

备注Remarks

调用SetSizes后,使用此成员函数覆盖标准工具栏高度。After calling SetSizes, use this member function to override the standard toolbar height. 如果高度太小,按钮将在底部被剪切。If the height is too small, the buttons will be clipped at the bottom.

如果未调用此功能,框架将使用按钮的大小来确定工具栏的高度。If this function is not called, the framework uses the size of the button to determine the toolbar height.

CToolBar::设置大小CToolBar::SetSizes

调用此成员函数将工具栏的按钮设置为大小按钮指定的大小(以像素为单位)。Call this member function to set the toolbar's buttons to the size, in pixels, specified in sizeButton.

void SetSizes(
    SIZE sizeButton,
    SIZE sizeImage);

参数Parameters

大小按钮sizeButton
每个按钮的大小(以像素为单位)。The size in pixels of each button.

大小图像sizeImage
每个图像的大小(以像素为单位)。The size in pixels of each image.

备注Remarks

sizeImage参数必须包含工具栏位图中图像的大小(以像素为单位)。The sizeImage parameter must contain the size, in pixels, of the images in the toolbar's bitmap. 尺寸按钮的尺寸必须足以容纳图像加上 7 像素的额外宽度和 6 像素的额外高度。The dimensions in sizeButton must be sufficient to hold the image plus 7 pixels extra in width and 6 pixels extra in height. 此功能还设置工具栏高度以适合按钮。This function also sets the toolbar height to fit the buttons.

仅对未遵循Windows 界面指南的软件设计建议按钮和图像大小的工具栏调用此成员函数。Call this member function only for toolbars that do not follow Windows Interface Guidelines for Software Design recommendations for button and image sizes.

示例Example

// This code fragment is taken from CMainFrame::OnCreate
// CMainFrame is derived from CMDIFrameWnd

// This example shows how to add text to toolbar buttons.
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE 
   | CBRS_TOP) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
   TRACE0("Failed to create toolbar\n");
   return -1;      // fail to create
}

//Show text on toolbar buttons.
VERIFY(m_wndToolBar.SetButtonText(0, _T("New")));
VERIFY(m_wndToolBar.SetButtonText(1, _T("Open")));
VERIFY(m_wndToolBar.SetButtonText(2, _T("Save")));
VERIFY(m_wndToolBar.SetButtonText(4, _T("Cut")));
VERIFY(m_wndToolBar.SetButtonText(5, _T("Copy")));
VERIFY(m_wndToolBar.SetButtonText(6, _T("Paste")));
VERIFY(m_wndToolBar.SetButtonText(8, _T("Print")));
VERIFY(m_wndToolBar.SetButtonText(9, _T("About")));

CRect temp;
m_wndToolBar.GetItemRect(0,&temp);
m_wndToolBar.SetSizes(CSize(temp.Width(),
   temp.Height()),CSize(16,15));

另请参阅See also

MFC 样品 CTRLBARSMFC Sample CTRLBARS
MFC 样品 DLGCBR32MFC Sample DLGCBR32
MFC 样品对接MFC Sample DOCKTOOL
CControlBar ClassCControlBar Class
层次结构图表Hierarchy Chart
CToolBarCtrl 类CToolBarCtrl Class
CControlBar ClassCControlBar Class