HOW TO:定義工具列按鈕的圖示
更新:2007 年 11 月
注意事項: |
---|
ToolStrip 控制項會取代並且將功能加入至 ToolBar 控制項;不過您也可以選擇保留 ToolBar 控制項,以提供回溯相容性 (Backward Compatibility) 以及供未來使用。 |
ToolBar 按鈕可以在按鈕中顯示圖示,方便使用者識別。這項功能是經由將影像加入至 ImageList 元件 (Windows Form) 元件,然後讓 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; }