方法: ツール バー ボタンのアイコンを定義する
注意
ToolStrip コントロールは、ToolBar コントロールに代わると共に追加の機能を提供します。ただし、ToolBar コントロールは、下位互換性を保つ目的および将来使用する目的で保持されます。
ToolBar ボタンの中にはアイコンを表示できます。これにより、ユーザーがボタンを識別しやすくなります。 これを行うには、ImageList Component コンポーネントに画像を追加し、その後、ImageList コンポーネントを ToolBar コントロールに関連付けます。
ツールバー ボタンのアイコンをプログラムによって設定するには
同じプロシージャ内で、ImageList コンポーネントに画像を割り当てます。
同じプロシージャ内で、ImageList コントロールを ToolBar コントロールに割り当て、個々のツールバー ボタンの ImageIndex プロパティを割り当てます。
次のコード例で画像の場所として設定されているパスは、My Documents フォルダーです。 このように設定できるのは、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; }
関連項目
.NET Desktop feedback
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示