工具栏中的Windows窗体用户控件自定义图标 [英] Windows form Usercontrol custom icon in toolbar

查看:284
本文介绍了工具栏中的Windows窗体用户控件自定义图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了usercontrol,我可以在工具栏中看到它,但是没有显示我自己的图标或位图图像用于usercontrol.我已经使用过[ToolboxBitmap(typeof(MyUserControl),"MyIcon.icon")],但仍显示默认图标.我还将图像的生成操作"设置为嵌入式资源.关于

解决方案

资源的名称空间是项目的名称空间+资源文件的目录结构("\"替换为". '').

如果"myicon.ico"位于项目的"images \ icons"文件夹中,并且项目的默认名称空间(在项目"属性中)为"shahzad",则图标的完整名称空间将是``shahzad.images.icons.myicon.ico''.

另外,由于.ico不能很好地缩放,因此建议对工具箱位图使用位图(.bmp).资源名称.将程序集加载到ILDasm中,以查看实际调用的资源.默认情况下,默认名称空间与项目名称相同,如果不进行更改,它将是您的类所放置的名称空间. IE.如果您的班级文件看起来像

using System.Windows.Forms;

namespace Shahzad.Controls {
 class MyUserControl : UserControl {
  ...
 }
}



...那么该资源可能会被称为''Shahzad.Controls.MyIcon.icon''.

ToolboxBitmapAttribute也添加了该命名空间.因此,请确保您的类名称空间与项目的默认名称空间相同.


在项目根文件夹中添加16 * 16位图图像.右键单击将构建操作"设置为嵌入式资源的图像.
命名空间MyNamespace
{
[ToolboxBitmap(typeof(MyNamespace.MyControlName),"MyNamespace.ImageName.bmp")]
公共局部类MyControlName:UserControl
{
}
}
重建解决方案后,右键单击工具箱",从bin/debug/中添加控件.dll文件.
现在,您将看到带有自定义图像的控件图标.

Shahzad ch.


I have created usercontrol and i can see in toolbar but my own icon or bitmap image is not displaying for usercontrol. I have already used [ToolboxBitmap(typeof(MyUserControl), "MyIcon.icon")] but still default icon is displayed. I have also set Build Action of image to embedded resource. Regards

解决方案

The namespace of the resource is the project''s namespace + the directory structure of the resource file (''\'' replace with a ''.'').

If ''myicon.ico'' is in ''images\icons'' folder in your project and the project''s default namespace (in Project properties) is ''shahzad'', then your icon''s full namespace would be ''shahzad.images.icons.myicon.ico''.

Also, it is recommended to use bitmaps (.bmp) for toolbox bitmap since .ico does not scale well.


If you built the assembly in Visual Studio, the default namespace will have been prepended to the resource name. Load the assembly into ILDasm to see what the resource is actually called. By default the default namespace is the same as the project name and it''s what your classes will have been placed in if you didn''t change them. I.e. if your class files look like

using System.Windows.Forms;

namespace Shahzad.Controls {
 class MyUserControl : UserControl {
  ...
 }
}



... then the resource will probably be called ''Shahzad.Controls.MyIcon.icon''.

EDIT: The ToolboxBitmapAttribute also adds this namespace. So make sure that your class namespace is the same as the default namespace for the project.


Add your 16*16 Bitmap Image at project root folder. Right click image set Build Action as Embedded Resource.
namespace MyNamespace
{
[ToolboxBitmap(typeof(MyNamespace.MyControlName), "MyNamespace.ImageName.bmp")]
public partial class MyControlName : UserControl
{
}
}
after rebuilding the solution right click Toolbox add your controls .dll file from bin/debug/
Now you will see your control icon with your custom image.

Shahzad ch.


这篇关于工具栏中的Windows窗体用户控件自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆