在工具箱中设置控件的图标(Windows 窗体 .NET)
你创建的控件始终会收到 Visual Studio 中工具箱窗口的通用图标。 但是,当你更改图标时,它会为控件增添专业感,并使其在工具箱中脱颖而出。 本文介绍如何设置控件的图标。
重要
面向 .NET 7 和 .NET 6 的桌面指南文档正在撰写中。
位图图标
Visual Studio 中工具箱窗口的图标必须符合某些标准,否则它们将被忽略或错误显示。
- 大小:控件的图标必须是 16x16 位图图像。
- 文件类型:图标可以是位图 (.bmp) 或 Windows 图标 (.ico) 文件。
- 透明度:洋红色(RGB:
255,0,255
,Hex:0xFF00FF
)呈现透明。 - 主题:Visual Studio 有多个主题,但每个主题要么是深色,要么是浅色。 你的图标应设计为浅色主题。 当 Visual Studio 使用深色主题时,图标中的深色和浅色将自动反转。
如何分配图标
图标分配给具有 ToolboxBitmapAttribute 特性的控件。 有关属性的详细信息,请参阅特性 (C#) 或特性概述 (Visual Basic)。
该特性设置在控件的类上,它具有三个不同的构造函数:
ToolboxBitmapAttribute(Type) - 此构造函数接收单个类型引用,并会从该类型尝试查找用作图标的嵌入资源。
该类型的 FullName 值用于尝试根据类型的名称查找相应的嵌入资源。 例如,如果引用了
MyProject.MyNamespace.CompassRose
类型,则特性将查找名为MyProject.MyNamespace.CompassRose.bmp
或MyProject.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
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈