Udostępnij przez


Instrukcje: definiowanie ikony dla przycisku ToolBar

Uwaga / Notatka

Kontrolka ToolStrip zastępuje i dodaje funkcje do kontrolki ToolBar; jednak kontrolka ToolBar jest zachowywana w celu zachowania zgodności z poprzednimi wersjami i użycia w przyszłości, jeśli wybierzesz.

ToolBar przyciski mogą wyświetlać w sobie ikony, co ułatwia ich identyfikację przez użytkowników. Można to osiągnąć przez dodanie obrazów do składnika ImageList, a następnie skojarzenie składnika z kontrolką .

Aby ustawić ikonę przycisku paska narzędzi programowo

  1. W procedurze utwórz wystąpienie składnika ImageList i kontrolki ToolBar.

  2. W tej samej procedurze przypisz obraz do składnika ImageList.

  3. W tej samej procedurze przypisz kontrolkę ImageList do kontrolki ToolBar i przypisz właściwość ImageIndex poszczególnych przycisków paska narzędzi.

    W poniższym przykładzie kodu ścieżka ustawiona dla lokalizacji obrazu to folder Moje dokumenty. Dzieje się tak, ponieważ można założyć, że większość komputerów z systemem operacyjnym Windows będzie zawierać ten katalog. Dzięki temu użytkownicy z minimalnymi poziomami dostępu systemu mogą bezpiecznie uruchamiać aplikację. W poniższym przykładzie zakłada się, że formularz ma już dodaną kontrolkę PictureBox.

    Po wykonaniu powyższych kroków należy napisać kod podobny do poniższego.

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

Zobacz także