HOW TO:為控制項提供工具箱點陣圖
如果想讓控制項的特殊圖示出現在 [工具箱] 中,您可使用 ToolboxBitmapAttribute 指定特定的影像。 這個類別是一種屬性 (Attribute),是可以附加到其他類別的特殊類別。 如需屬性的詳細資訊,請參閱 Visual Basic 的 Attributes Overview in Visual Basic和 Visual C# 的屬性 (C# 和 Visual Basic)。
您可使用 ToolboxBitmapAttribute 來指定字串,此字串表示 16 x 16 像素點陣圖的路徑和檔名。 將控制項加入 [工具箱] 時,這個點陣圖就會出現在控制項旁。 您也可以指定 Type,而在這種情況下就會載入與該型別關聯的點陣圖。 如果同時指定了 Type 和字串,控制項就會搜尋名稱是由組件中的字串參數所指定的影像資源,此組件包含 Type 參數指定的型別。
若要為您的控制項指定工具箱點陣圖
在 Visual Basic 的 Class 關鍵字之前,以及在 Visual C# 的類別宣告 (Class Declaration) 上方,將 ToolboxBitmapAttribute 加入至控制項的類別宣告。
' Specifies the bitmap associated with the Button type. <ToolboxBitmap(GetType(Button))> Class MyControl1 ' Specifies a bitmap file. End Class <ToolboxBitmap("C:\Documents and Settings\Joe\MyPics\myImage.bmp")> _ Class MyControl2 End Class ' Specifies a type that indicates the assembly to search, and the name ' of an image resource to look for. <ToolboxBitmap(GetType(MyControl), "MyControlBitmap")> Class MyControl End Class
// Specifies the bitmap associated with the Button type. [ToolboxBitmap(typeof(Button))] class MyControl1 : UserControl { } // Specifies a bitmap file. [ToolboxBitmap(@"C:\Documents and Settings\Joe\MyPics\myImage.bmp")] class MyControl2 : UserControl { } // Specifies a type that indicates the assembly to search, and the name // of an image resource to look for. [ToolboxBitmap(typeof(MyControl), "MyControlBitmap")] class MyControl : UserControl { }
// Specifies the bitmap associated with the Button type. /** @attribute ToolboxBitmap(Button.class) */ class MyControl1 extends UserControl { } // Specifies a bitmap file. /** @attribute ToolboxBitmap("C:\\Documents and Settings\\Joe\\MyPics\\myImage.bmp")*/ class MyControl2 extends UserControl { } // Specifies a type that indicates the assembly to search, and the name // of an image resource to look for. /* @attribute ToolboxBitmap(MyControl.class, "MyControlBitmap") */ class MyControl extends UserControl { }
重建專案。
注意事項
[工具箱] 中沒有出現用來自動產生控制項和元件的點陣圖。 若要看到點陣圖,請使用 [選擇工具箱項目] 對話方塊重新載入該控制項。 如需詳細資訊,請參閱逐步解說:自動將自訂元件填入工具箱。