我已经在这方面工作了一段时间,但似乎无法弄清楚!
我有一个看起来像下面布局的解决方案...
Solution 'Gen' (2 projects)
'-> MyControls
'-> graphics
'-> UserControl1.bmp
'-> UserControl1.cs
'-> Gen
'-> MainGUI.cs
我只想制作一个工具箱图标,这样我就可以在工具栏上看到一个漂亮的图标,而不是像蓝色的齿轮(默认工具箱图标)。
我目前采取的步骤...
<强>1。创建位图图像:
我制作了一个 16x16 像素的图像,即 16 色(4 位)。
<强>2。将图像作为资源嵌入:
单击解决方案资源管理器窗口中的图像,然后在属性窗口中将构建操作更改为“嵌入式资源”。
<强>3。添加用于修复 GetImageFromResource() 方法中的错误的内部类:
在命名空间声明之前,我添加了以下行...
internal class resfinder
{
}
<强>4。添加类属性“ToolboxBitmap”:
类声明之上...
public partial class UserControl1 : UserControl
我添加了以下行...
[ToolboxBitmap(typeof(resfinder), "Gen.graphics.UserControl1.bmp")]
<强>5。构建项目:
在构建菜单下,我使用了“重建解决方案”。
整个代码块:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
internal class resfinder
{
}
namespace Gen
{
[ToolboxBitmap(typeof(resfinder), "Gen.graphics.UserControl1.bmp")]
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
}
}
结果:
当我尝试在 MainGUI.cs 窗体上使用它时,我仍然有类似蓝色的小齿轮作为工具箱图标。
来源:
最佳答案
看来答案很简单……其实我都做对了!
不过有一点值得注意...
The custom toolbox icon will NOT show up if you are using a reference to a project in the same solution.
要修复/解决此问题,您需要执行以下操作...
Add a reference to the compiled (product of the custom control project) DLL file instead.
我最终在编译后删除了自定义控件项目,然后添加了对我想使用它的项目的引用。在我这样做之后,我右键单击工具箱,然后单击“选择项目...”。然后单击浏览并从那里加载 DLL。
现在自定义工具箱图标显示得很好!
关于c# - 为我的自定义用户控件创建自定义工具箱图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27513795/