MSDN:代码示例
//
Specifies the bitmap associated with the Button type.
[ToolboxBitmap( typeof (Button))]
class MyControl1 : UserControl
... {
}
// Specifies a bitmap file.
[ToolboxBitmap( @" C:Documents and SettingsJoeMyPicsmyImage.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
... {
}
[ToolboxBitmap( typeof (Button))]
class MyControl1 : UserControl
... {
}
// Specifies a bitmap file.
[ToolboxBitmap( @" C:Documents and SettingsJoeMyPicsmyImage.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
... {
}
第一种方式比较方便和常用.
[ToolboxBitmap(typeof(Button))]
class MyControl1:UserControl{}
这里设置当前控件的图标为System.Window.Forms.Button图标.当然我们可以设置成其他已经引用的控件的图标(如:TextBox),改个类名即可.如果要设置自己的图标可以按照以下步骤:
1.为当前工程添加一个资源文件(右键工程菜单->添加项-选择资源文件或查看工程属性(资源)时点击链接自动生成.)
2.添加一个图片文件(文件格式为bmp,大小16*16),注意文件名要跟类名一致(如ControlClassName.bmp)
3.设置图片文件的属性(生成操作:嵌入的资源)
4.在类定义前加上[ToolboxBitmap(typeof(ControlClassName))].
第二种很简单直接指定文件的路径就可以,缺点是必须附带图标文件,一旦删除会出错.
第三种和第一种类似,只是可以指定图标的文件名.