方法: ToolBar ボタンのアイコンを定義する
手記
ToolStrip コントロールは、ToolBar コントロールに代わって機能を追加します。ただし、ToolBar コントロールは、下位互換性と将来の使用の両方で保持されます (選択した場合)。
ToolBar ボタンは、ユーザーが簡単に識別できるように、その中にアイコンを表示できます。 これは、ImageList コンポーネント コンポーネントにイメージを追加し、ImageList コンポーネントを ToolBar コントロールに関連付けることによって実現されます。
ツール バー ボタンのアイコンをプログラムで設定するには
同じ手順で、ImageList コンポーネントにイメージを割り当てます。
同じ手順で、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; }
関連項目
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback