如何:定义工具栏按钮的图标

注释

ToolStrip 控件替换并添加 ToolBar 控件的功能;但是,如果选择,则保留 ToolBar 控件以实现后向兼容性和将来使用。

ToolBar 按钮能够显示图标,方便用户识别。 这可以通过将图像添加到 ImageList 组件中,然后将ImageList组件与ToolBar控件相关联来实现。

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

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

  2. 在同一过程中,将图像分配给ImageList组件。

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

    在以下代码示例中,为图像位置设置的路径是“我的文档” 文件夹 。 这样做是因为你可以假定运行 Windows 操作系统的大多数计算机将包含此目录。 这还允许用户使用最少的系统访问级别安全地运行应用程序。 以下示例假定窗体已添加控件 PictureBox

    按照上述步骤操作,您应该编写出与下面所示类似的代码。

    Public Sub InitializeMyToolBar()
    ' Instantiate an ImageList component and a ToolBar control.
       Dim ToolBar1 as New ToolBar
       Dim ImageList1 as New ImageList
    ' Assign an image to the ImageList component.
    ' You should replace the bold image
    ' in the sample below with an icon of your own choosing.
       Dim myImage As System.Drawing.Image = _
          Image.FromFile Image.FromFile _
          (System.Environment.GetFolderPath _
          (System.Environment.SpecialFolder.Personal) _
          & "\Image.gif")
       ImageList1.Images.Add(myImage)
    ' Create a ToolBarButton.
       Dim ToolBarButton1 As New ToolBarButton()
    ' Add the ToolBarButton to the ToolBar.
       ToolBar1.Buttons.Add(toolBarButton1)
    ' Assign an ImageList to the ToolBar.
       ToolBar1.ImageList = ImageList1
    ' Assign the ImageIndex property of the ToolBarButton.
       ToolBarButton1.ImageIndex = 0
    End Sub
    
    public void InitializeMyToolBar()
    {
       // Instantiate an ImageList component and a ToolBar control.
       ToolBar toolBar1 = new  ToolBar();
       ImageList imageList1 = new ImageList();
       // Assign an image to the ImageList component.
       // You should replace the bold image
       // in the sample below with an icon of your own choosing.
       // Note the escape character used (@) when specifying the path.
       Image myImage = Image.FromFile
       (System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + @"\Image.gif");
       imageList1.Images.Add(myImage);
       // Create a ToolBarButton.
       ToolBarButton toolBarButton1 = new ToolBarButton();
       // Add the ToolBarButton to the ToolBar.
       toolBar1.Buttons.Add(toolBarButton1);
       // Assign an ImageList to the ToolBar.
       toolBar1.ImageList = imageList1;
       // Assign ImageIndex property of the ToolBarButton.
       toolBarButton1.ImageIndex = 0;
    }
    
    public:
       void InitializeMyToolBar()
       {
          // Instantiate an ImageList component and a ToolBar control.
          ToolBar ^ toolBar1 = gcnew  ToolBar();
          ImageList ^ imageList1 = gcnew ImageList();
          // Assign an image to the ImageList component.
          // You should replace the bold image
          // in the sample below with an icon of your own choosing.
          Image ^ myImage = Image::FromFile(String::Concat
             (System::Environment::GetFolderPath
             (System::Environment::SpecialFolder::Personal),
             "\\Image.gif"));
          imageList1->Images->Add(myImage);
          // Create a ToolBarButton.
          ToolBarButton ^ toolBarButton1 = gcnew ToolBarButton();
          // Add the ToolBarButton to the ToolBar.
          toolBar1->Buttons->Add(toolBarButton1);
          // Assign an ImageList to the ToolBar.
          toolBar1->ImageList = imageList1;
          // Assign ImageIndex property of the ToolBarButton.
          toolBarButton1->ImageIndex = 0;
       }
    

另请参阅