共用方式為


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

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

重要

.NET 7 和 .NET 6 的桌面指南檔正在建置中。

點陣圖圖示

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

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

如何指派圖示

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

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

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

    FullName 別的值是用來嘗試根據型別的名稱尋找對應的內嵌資源。 例如,如果 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.CompasRoseIcon.bmp resource
    // in the assembly containing the type CompassRose
    [ToolboxBitmap(typeof(CompassRose), "WinFormsApp1.Resources.CompasRoseIcon.bmp")]
    public partial class CompassRose : UserControl
    {
        // Code for the control
    }
    
    ' Loads the icon from the WinFormsApp1.Resources.CompasRoseIcon.bmp resource
    ' in the assembly containing the type CompassRose
    <ToolboxBitmap(GetType(CompassRose), "WinFormsApp1.Resources.CompasRoseIcon.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