ToolboxBitmapAttribute——显示自定义图标

来源:互联网 发布:大数据算法数学模型 编辑:程序博客网 时间:2024/05/16 09:27

通过ToolboxBitmapAttribute 将自定义工具箱图标添加到 Windows 窗体控件,有三种方式:

  NameDescriptionPublic methodToolboxBitmapAttribute(String)Initializes a new ToolboxBitmapAttribute object with an image from a specified file.Public methodToolboxBitmapAttribute(Type)Initializes a new ToolboxBitmapAttribute object based on a 16 x 16 bitmap that is embedded as a resource in a specified assembly.Public methodToolboxBitmapAttribute(Type, String)Initializes a new ToolboxBitmapAttribute object based on a 16 by 16 bitmap that is embedded as a resource in a specified assembly.

 

第一种方式是使用图标绝对路径来进行配置(注意:相对路径不可用!),示例如下:

[System.Drawing.ToolboxBitmap("c://MyControl.bmp")]
public class MyControl: System.Windows.Forms.UserControl

{

.......
}


第二种方式是使用原因系统控件图标来进行设置,示例如下:

  [ToolboxBitmap(typeof(Button))]
  [DisplayName("按钮")]
  public class ButtonEx : Button
   {

  }


 

可是这两种方式一般都不能满足开发需求,本人推荐使用第三种方法:采用嵌入式资源图标的方式。示例如下: 

 namespace Toolbox
{

  [ToolboxBitmap(typeof(MyControl ), "MyControl .gif")]
   public class MyControl : System.Windows.Forms.UserControl

  {
  }

}

不过使用这种方法需要注意以下几点:

1. 自定义图标需设置为嵌入式资源:右键点击自定义图标,找到“属性”项,找到“生成操作”属性。默认状态,设置为“内容”。单击 “内容”,然后选择“嵌入的资源”。当编译项目时,这可以为在您的程序集资源包括图像。

2. 自定义图标需与自定义控件具有同一个命名空间,在上例中,系统会强制进行寻找资源中已编译的程序集命名 Toolbox.MyControl.gif 。

 

 

msdn参考资料:

http://support.microsoft.com/kb/311315

http://msdn.microsoft.com/en-us/library/system.drawing.toolboxbitmapattribute(VS.80).aspx 

http://msdn.microsoft.com/en-us/library/4wk1wc0a(VS.71).aspx 

 

原创粉丝点击