共用方式為


在工具箱設定控制元件的圖示 (Windows Forms .NET)

您建立的控件一律會收到 Visual Studio 中 [工具箱] 視窗的一般圖示。 不過,當您變更圖示時,它會在控件中新增專業精神,並讓它在工具箱中脫穎而出。 本文會教導您如何設定控件的圖示。

點陣圖圖示

Visual Studio 中 [工具箱] 視窗的圖示必須符合特定標準,否則會忽略或顯示不正確。

  • 大小:控件的圖示必須是16x16點陣陣圖影像。
  • 檔案類型:圖示可以是點陣圖 (.bmp) 或 Windows 圖示 (.ico) 檔案。
  • 透明度:洋紅色彩 (RGB: 255,0,255、十六進位: 0xFF00FF) 會呈現透明。
  • 主題:Visual Studio 有多個主題,但每個主題都會被視為深色或淺色。 您的圖示應該針對淺色主題進行設計。 當 Visual Studio 使用深色主題時,圖示中的深色和淺色會自動反轉。

如何指派圖示

圖示會指派給具有 屬性的 ToolboxBitmapAttribute 控件。 如需屬性的詳細資訊,請參閱屬性 (C#)屬性概觀 (Visual Basic)。

提示

您可以從 GitHub 下載範例圖示

屬性是在控件的 類別上設定,而且有三個不同的建構函式:

  • ToolboxBitmapAttribute(Type)—此建構函式會採用單一類型參考,並從該類型嘗試尋找要作為圖示使用的內嵌資源。

    型別的 FullName 是用來查閱 該型別元件中的內嵌資源 ,格式如下: {project-name}.{namespace-path}.{type-name}{.bmp|.ico}。 例如,如果參考類型 MyProject.MyNamespace.CompassRose ,屬性會尋找名為 MyProject.MyNamespace.CompassRose.bmpMyProject.MyNamespace.CompassRose.ico的內嵌資源。

    // Looks for a CompassRose.bmp or CompassRose.ico embedded resource in the
    // same namespace as the CompassRose type.
    [ToolboxBitmap(typeof(CompassRose))]
    public partial class CompassRose : UserControl
    {
        // Code for the control
    }
    
    ' Looks for a CompassRose.bmp or CompassRose.ico embedded resource in the
    ' same namespace as the CompassRose type.
    <ToolboxBitmap(GetType(CompassRose))>
    Public Class CompassRose
        ' Code for the control
    End Class
    
  • ToolboxBitmapAttribute(Type, String)—此建構函式接受兩個參數。 第一個參數是類型,第二個參數是該類型元件中內嵌資源的命名空間和名稱

    // Loads the icon from the WinFormsApp1.Resources.CompassRose.bmp resource
    // in the assembly containing the type CompassRose
    [ToolboxBitmap(typeof(CompassRose), "WinFormsApp1.Resources.CompassRose.bmp")]
    public partial class CompassRose : UserControl
    {
        // Code for the control
    }
    
    ' Loads the icon from the WinFormsApp1.Resources.CompassRose.bmp resource
    ' in the assembly containing the type CompassRose
    <ToolboxBitmap(GetType(CompassRose), "WinFormsApp1.Resources.CompassRose.bmp")>
    Public Class CompassRose
        ' Code for the control
    End Class
    
  • ToolboxBitmapAttribute(String)—此建構函式會採用單一字串參數,這是圖示檔的絕對路徑。

    // Loads the icon from a file on disk
    [ToolboxBitmap(@"C:\Files\Resources\MyIcon.bmp")]
    public partial class CompassRose : UserControl
    {
        // Code for the control
    }
    
    ' Loads the icon from a file on disk
    <ToolboxBitmap("C:\Files\Resources\MyIcon.bmp")>
    Public Class CompassRose
        ' Code for the control
    End Class