Partilhar via


Como: Definir um ícone para um botão da barra de ferramentas

Observação

O controle ToolStrip substitui e adiciona funcionalidade ao controle ToolBar; no entanto, o controle ToolBar é mantido para compatibilidade com versões anteriores e uso futuro, se você escolher.

Os botões ToolBar são capazes de exibir ícones no seu interior para fácil identificação pelos utilizadores. Isso é conseguido adicionando imagens ao componente ImageList e, em seguida, associando o componente ao controle .

Para definir um ícone para um botão da barra de ferramentas programaticamente

  1. Em um procedimento, instancie um componente ImageList e um controle ToolBar.

  2. No mesmo procedimento, atribua uma imagem ao componente ImageList.

  3. No mesmo procedimento, atribua o controle ImageList ao controle ToolBar e atribua a propriedade ImageIndex dos botões individuais da barra de ferramentas.

    No exemplo de código a seguir, o caminho definido para o local da imagem é a pasta My Documents. Isso é feito, porque você pode assumir que a maioria dos computadores que executam o sistema operacional Windows incluirá esse diretório. Isso também permite que usuários com níveis mínimos de acesso ao sistema executem o aplicativo com segurança. O exemplo abaixo assume um formulário com um controle PictureBox já adicionado.

    Seguindo as etapas acima, você deve ter escrito código semelhante ao exibido abaixo.

    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;
       }
    

Ver também