Notes
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Remarque
Le contrôle ToolStrip remplace et ajoute des fonctionnalités au contrôle ToolBar ; toutefois, le contrôle ToolBar est conservé à la fois pour la compatibilité descendante et l’utilisation ultérieure, si vous le choisissez.
ToolBar les boutons sont en mesure d’afficher des icônes en eux pour faciliter l’identification par les utilisateurs. Pour ce faire, vous pouvez ajouter des images au composant Composant ImageList , puis associer le ImageList composant au ToolBar contrôle.
Pour définir une icône pour un bouton de barre d’outils par programmation
Dans une procédure, instanciez un ImageList composant et un ToolBar contrôle.
Dans la même procédure, affectez une image au ImageList composant.
Dans la même procédure, affectez le contrôle ImageList au contrôle ToolBar et attribuez la propriété ImageIndex à chaque bouton individuel de la barre d’outils.
Dans l’exemple de code suivant, le chemin d’accès défini pour l’emplacement de l’image est le dossier Mes documents . Cette opération est effectuée, car vous pouvez supposer que la plupart des ordinateurs exécutant le système d’exploitation Windows incluront ce répertoire. Cela permet également aux utilisateurs disposant de niveaux d’accès système minimal d’exécuter l’application en toute sécurité. L’exemple ci-dessous suppose un formulaire avec un contrôle PictureBox déjà ajouté.
En suivant les étapes ci-dessus, vous devez avoir écrit du code similaire à celui affiché ci-dessous.
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; }
Voir aussi
.NET Desktop feedback