永远的学生

qindgfly

导航

如何:为控件提供工具箱位图

如果希望在“工具箱”中为控件显示特殊图标,可以通过使用 ToolboxBitmapAttribute 来指定一个特定的图像。此类是一种属性,这是一种可以附加到其他类上的特殊类。有关属性的更多信息,对于 Visual Basic 请参见 Visual Basic 中的属性概述,对于 Visual C# 请参见 属性(C# 编程指南)

通过使用 ToolboxBitmapAttribute,可以指定一个字符串来指示一个 16 x 16 像素位图的路径和文件名。此位图在添加到“工具箱”后显示在对应的控件旁边。还可以指定 Type,在这种情况下会加载与该类型关联的位图。如果您同时指定 Type 和字符串,则控件在包含由 Type 参数指定的类型的程序集中搜索其名称由 String 参数指定的图像资源。

// 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
{
}





注意:

对于自动生成的控件和组件,位图将不出现在工具箱中。若要查看位图,请使用“选择工具箱项”对话框重新加载控件。



posted on 2008-12-27 13:03  东国先生  阅读(332)  评论(0编辑  收藏  举报