Windows 工具栏ToolBar

ToolBar表示一个 Windows 工具栏。

ToolBar 控件用于显示可显示为标准按钮、切换式按钮或下拉式按钮的 ToolBarButton 控件。可以为按钮分配图像,方法是创建一个 ImageList,将它分配给工具栏的 ImageList 属性,然后将图像索引值分配给每个 ToolBarButton ImageIndex 属性。然后可以通过设置 ToolBarButton Text 属性,将文本指定为显示在图像的下方或右边。

将工具栏的 Appearance 属性设置为 Flat,为工具栏及其按钮赋予平面外观。当鼠标指针移动到按钮上时,按钮的外观变为三维样式。通过使用分隔符可以将工具栏按钮划分成多个逻辑组。分隔符是 Style 属性设置为 ToolBarButtonStyle.Separator 的工具栏按钮。当工具栏具有平面外观时,按钮之间的按钮分隔符显示为线,而不是间隔。如果将 Appearance 属性设置为 Normal,则工具栏按钮的外观呈现凸起的三维效果。

如果指定 ButtonSize 属性的值,则工具栏中的所有按钮都被限制为指定的大小。否则,这些按钮将根据其内容调整大小,并且 ButtonSize 属性返回最大按钮的初始大小。

若要创建将在 ToolBar 上显示的 ToolBarButton 控件的集合,请使用 Buttons 属性的 Add Insert 方法逐个添加这些按钮。

.NET Framework 精简版平台说明 一个 Form 只支持一个 ToolBar,尝试添加额外的 ToolBar 会引发 NotSupportedException

不支持向除 Form 以外的任何其他控件中添加 ToolBar,例如向 Panel 中添加。

示例

下面的示例创建一个 ToolBar 和三个 ToolBarButton 控件。这些工具栏按钮将被分配到按钮集合,该集合又被分配到工具栏,而工具栏将被添加到窗体上。在工具栏的 ButtonClick 事件上计算 ToolBarButtonClickEventArgs Button 属性,并打开适当的对话框。这段代码假定已经创建一个 Form、一个 OpenFileDialog、一个 SaveFileDialog 和一个 PrintDialog

public void InitializeMyToolBar()

 {

    // Create and initialize the ToolBar and ToolBarButton controls.

    toolBar1 = new ToolBar();

    ToolBarButton toolBarButton1 = new ToolBarButton();

    ToolBarButton toolBarButton2 = new ToolBarButton();

    ToolBarButton toolBarButton3 = new ToolBarButton();

 

    // Set the Text properties of the ToolBarButton controls.

    toolBarButton1.Text = "Open";

    toolBarButton2.Text = "Save";

    toolBarButton3.Text = "Print";

 

    // Add the ToolBarButton controls to the ToolBar.

    toolBar1.Buttons.Add(toolBarButton1);

    toolBar1.Buttons.Add(toolBarButton2);

    toolBar1.Buttons.Add(toolBarButton3);

   

    // Add the event-handler delegate.

    toolBar1.ButtonClick += new ToolBarButtonClickEventHandler (

       this.toolBar1_ButtonClick);

   

    // Add the ToolBar to the Form.

    Controls.Add(toolBar1);

 }

 

 protected void toolBar1_ButtonClick (

                         Object sender,

                         ToolBarButtonClickEventArgs e)

 {

   // Evaluate the Button property to determine which button was clicked.

   switch(toolBar1.Buttons.IndexOf(e.Button))

   {

      case 0:

         openFileDialog1.ShowDialog();

         // Insert code to open the file.

         break;

      case 1:

         saveFileDialog1.ShowDialog();

         // Insert code to save the file.

         break;

      case 2:

         printDialog1.ShowDialog();

         // Insert code to print the file.   

         break;

    }

 }

 

Windows 窗体 Toolbar 控件介绍

Windows 窗体 ToolBar 控件在窗体中用作控制条,用以显示一列下拉菜单和可激活命令的位图按钮。这样,单击一个工具栏按钮可与选择一个菜单命令等效。可将按钮配置为以普通按钮、下拉菜单或分隔符等形式显示和表现。通常情况下,工具栏包含的按钮和菜单与应用程序菜单结构中的项相对应,以提供对应用程序的常用功能和命令的快速访问。

ToolBar 控件通常沿其父窗口顶部停靠,但是也可以将它停靠到窗口的任一边上。当用户将鼠标指针指向工具栏按钮时,工具栏可以显示工具提示。工具提示是一个小的弹出式窗口,用以简述按钮或菜单的用途。若要显示工具提示,必须将 ShowToolTips 属性设置为 true

注意   某些应用程序的特色是具有与工具栏非常类似的控件,这些控件可以浮动在应用程序窗口之上并可以重新定位。Windows 窗体 ToolBar 控件不能执行这些操作。

Appearance 属性设置为 Normal 时,工具栏按钮以凸起和三维方式显示。若要为工具栏及其按钮提供一个平面外观,可将工具栏的 Appearance 属性设置为 Flat。当鼠标指针移动到平面按钮时,该按钮的外观变为三维形状。可以使用分隔符将工具栏按钮划分成多个逻辑组。分隔符是 Style 属性设置为 Separator 的工具栏按钮。它在工具栏上显示为空白。当工具栏具有平面外观时,按钮分隔符显示为直线而不是按钮之间的空白。

ToolBar 控件使您可以通过向 Buttons 集合添加 Button 对象来创建工具栏。可以使用集合编辑器向 ToolBar 控件添加按钮;应为每个 Button 对象指定文本或图像,也可以两者都指定。图像由一个关联的 ImageList 组件提供。运行时,可使用 Add 方法和 Remove 方法分别向 ToolBarButtonCollection 中添加按钮或从中移除按钮。若要为 Toolbar 的按钮进行编程,请向 ToolBar ButtonClick 事件添加代码;可使用 ToolBarButtonClickEventArgs 类的 Button 属性来确定所单击的按钮。

 

向工具栏添加按钮

ToolBar 控件的一个完整部分是向它添加的按钮。使用这些按钮可以很容易地访问菜单命令;或者也可以将它们放在应用程序用户界面的另一个区域,向用户公开菜单结构中不可用的命令。

下面的示例假定已向 Windows 窗体 (Form1) 添加了一个工具栏”(ToolBar) 控件。有关详细信息,请参见 Windows 窗体添加控件

在设计时添加按钮

1.            属性窗口顶部的下拉列表中,选择已向窗体添加的工具栏”(ToolBar) 控件。

2.            单击 Buttons 属性可以选择该属性,然后单击省略号按钮 ( ) 以打开 ToolBarButton 集合编辑器。

3.            使用添加移除按钮分别向工具栏”(ToolBar) 控件添加按钮和从中移除按钮。

4.            配置编辑器右侧窗格中出现的属性窗口中单个按钮的属性。要考虑的重要属性包括:

属性

说明

DropDownMenu

设置要在下拉工具栏按钮中显示的菜单。工具栏按钮的 Style 属性必须设置为 DropDown Button。该属性将 ContextMenu 类的一个实例作为引用。有关示例,请参见下面的代码实例。

PartialPush

设置切换样式的按钮是否为部分下压。工具栏按钮的 Style 属性必须设置为 ToggleButton

Pushed

设置切换样式的工具栏按钮当前是否处于下压状态。工具栏按钮的 Style 属性必须设置为 ToggleButton PushButton

Style

设置工具栏按钮的样式。必须是 ToolBarButtonStyle 枚举中的值之一。

Text

按钮显示的文本字符串。

ToolTipText

显示为按钮的工具提示的文本。

5.             单击确定可关闭对话框并可创建您指定的面板。

以编程方式添加按钮

1.             在过程中,创建工具栏按钮,方法是将这些按钮添加到 ToolBarButtons 集合中。

2.             通过 Buttons 属性传递按钮的索引来指定单个按钮的属性设置。

下面的示例假定一个已添加了工具栏”(ToolBar) 控件的窗体。

注意   ToolBarButtons 集合是一个从零开始的集合,所以应按相应的初始值编写代码。

 

// C#

public void CreateToolBarButtons()

{

   // Create buttons and set text property.

   toolBar1.Buttons.Add("One");

   toolBar1.Buttons.Add("Two");

   toolBar1.Buttons.Add("Three");

   toolBar1.Buttons.Add("Four");

 

   // Set properties of StatusBar panels.

   // Set Style property.

   toolBar1.Buttons[0].Style = ToolBarButtonStyle.PushButton;

   toolBar1.Buttons[1].Style = ToolBarButtonStyle.Separator;

   toolBar1.Buttons[2].Style = ToolBarButtonStyle.ToggleButton;

   toolBar1.Buttons[3].Style = ToolBarButtonStyle.DropDownButton;

 

   // Set the ToggleButton's PartialPush property.

   toolBar1.Buttons[2].PartialPush = true;

 

   // Instantiate a ContextMenu component and menu items.

   // Set the DropDownButton's DropDownMenu property to

   // the context menu.

   ContextMenu cm = new ContextMenu();

   MenuItem miOne = new MenuItem("One");

   MenuItem miTwo = new MenuItem("Two");

   MenuItem miThree = new MenuItem("Three");

   cm.MenuItems.Add(miOne);

   cm.MenuItems.Add(miTwo);

   cm.MenuItems.Add(miThree);

 

   toolBar1.Buttons[3].DropDownMenu = cm;

   // Set the PushButton's Pushed property.

   toolBar1.Buttons[0].Pushed = true;

   // Set the ToolTipText property of 1 of the buttons.

   toolBar1.Buttons[1].ToolTipText = "Button 2";

}

定义工具栏按钮的图标

ToolBar 按钮中能够显示图标,目的是便于用户进行识别。这是通过以下操作实现的:向 ImageList 组件添加图像,然后使 ImageList 组件与工具栏”(ToolBar) 控件关联。

在设计时设置工具栏按钮的图标

1.             ImageList 组件从工具箱拖到窗体上。

2.             属性窗口中,单击 Images 属性,并向 ImageList 控件添加图像。有关详细信息,请参见使用 Windows 窗体 ImageList 组件添加或移除图像

3.             ToolBar 控件从工具箱拖到窗体上。

4.             属性窗口中,将 ToolBar 控件的 ImageList 属性设置为早先添加的 ImageList 控件。

5.             单击 ToolBar 控件的 Buttons 属性选择该属性,然后单击省略号按钮 ( ) 打开 ToolBarButton 集合编辑器。

6.             使用添加按钮向 ToolBar 控件添加按钮。

7.             ToolBarButton 集合编辑器右侧窗格中出现的属性窗口中,将每个工具栏按钮的 ImageIndex 属性设置为列表中的值之一,该值是从向 ImageList 组件添加的图像中提取的。

以编程方式设置工具栏按钮的图标

此示例假定已实例化一个 ImageList 组件和一个 ToolBar 控件,并且至少已向 ImageList 组件分配了一个图像。有关详细信息,请参见 Image

1.             在过程中,实例化一个 ImageList 组件和一个 ToolBar 控件。

2.             在同一过程中,向该 ImageList 组件分配一个图像。

3.             在同一过程中,向该 ToolBar 控件分配 ImageList 控件,并分配单个工具栏按钮的 ImageIndex 属性。

在下面的示例中,图像位置的路径设置是 My Documents 文件夹。这样做是因为可假定大多数运行 Windows 操作系统的计算机都包含此目录。这还将允许具有最低系统访问级别的用户安全地运行应用程序。下面的示例假定窗体已添加了 PictureBox 控件。

按照上述步骤,您应该已经编写出类似于下面显示的代码。

1.                  // C#
   
   
2.                  public void InitializeMyToolBar()
   
   
3.                  {
   
   
4.                     // Instantiate an ImageList component and a ToolBar control.
   
   
5.                     ToolBar toolBar1 = new  ToolBar(); 
   
   
6.                     ImageList imageList1 = new ImageList();
   
   
7.                     // Assign an image to the ImageList component.
   
   
8.                     // You should replace the bold image 
   
   
9.                     // in the sample below with an icon of your own choosing.
   
   
10.                // Note the escape character used (@) when specifying the path.
   
   
11.                Image myImage = Image.FromFile
   
   
12.                (System.Environment.GetFolderPath
   
   
13.                (System.Environment.SpecialFolder.Personal)
   
   
14.                + @"/Image.gif");
   
   
15.                imageList1.Images.Add(myImage);
   
   
16.                // Create a ToolBarButton.
   
   
17.                ToolBarButton toolBarButton1 = new ToolBarButton();
   
   
18.                // Add the ToolBarButton to the ToolBar.
   
   
19.                toolBar1.Buttons.Add(toolBarButton1);
   
   
20.                // Assign an ImageList to the ToolBar.
   
   
21.                toolBar1.ImageList = imageList1;
   
   
22.                // Assign ImageIndex property of the ToolBarButton.
   
   
23.                toolBarButton1.ImageIndex = 0;
   
   
24.             }
   
   
25.             
   
   
    
     
   
   

触发工具栏按钮的菜单事件

如果 Windows 窗体以带有工具栏按钮的 ToolBar 控件为特色,则需要知道用户单击的是哪个按钮。

在发生 ToolBar 控件的 ButtonClick 事件时,可以计算 ToolBarButtonClickEventArgs 类的 Button 属性。下面的示例中显示一个消息框,该框指示单击了哪个按钮。有关详细信息,请参见 MessageBox

下面的示例假定已向 Windows 窗体添加了一个 ToolBar 控件。

处理工具栏上的 Click 事件

1.             在过程中,向 ToolBar 控件添加工具栏按钮。

2.    // C#
    
    
3.    public void ToolBarConfig() 
    
    
4.    {
    
    
5.       toolBar1.Buttons.Add(new ToolBarButton("One"));
    
    
6.       toolBar1.Buttons.Add(new ToolBarButton("Two"));
    
    
7.       toolBar1.Buttons.Add(new ToolBarButton("Three"));
    
    
8.    
    
    
     
      
    
    
9.       toolBar1.ButtonClick += 
    
    
10.      new ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
    
    
11.}
    
    
12.
    
    
     
      
    
    

 

2   ToolBar 控件的 ButtonClick 事件添加事件处理程序。使用 Select Case 语句和 ToolBarButtonClickEventArgs 类来确定单击的工具栏按钮。并据此显示相应的消息框。

注意   在这个示例中,消息框只是用作占位符。可随意添加在单击工具栏按钮时执行的其他代码。

1.                   // C#
    
    
2.                   protected void toolBar1_ButtonClick(object sender,
    
    
3.                   ToolBarButtonClickEventArgs e)
    
    
4.                   {
    
    
5.                      // Evaluate the Button property of the ToolBarButtonClickEventArgs
    
    
6.                      // to determine which button was clicked.
    
    
7.                      switch (toolBar1.Buttons.IndexOf(e.Button))
    
    
8.                      {
    
    
9.                         case 0 :
    
    
10.                        MessageBox.Show("First toolbar button clicked");
    
    
11.                        break;
    
    
12.                     case 1 :
    
    
13.                        MessageBox.Show("Second toolbar button clicked");
    
    
14.                        break;
    
    
15.                     case 2 :
    
    
16.                        MessageBox.Show("Third toolbar button clicked");
    
    
17.                        break;
    
    
18.                  }
    
    
19.               }
    
    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值