c# - 为我的自定义用户控件创建自定义工具箱图标

标签 c# bitmap user-controls

我已经在这方面工作了一段时间,但似乎无法弄清楚!

我有一个看起来像下面布局的解决方案...

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/

相关文章:

c# - IIS 池回收、池重启、应用程序重启、web.config 更新 - 全局 asax

c# - 如何从 ElapsedEventHandler 中调用异步方法

Android:DialogFragment 中的位图大小超过 32 位

android - 如何将 decodeSampledBitmapFromResource 与下载的图片一起使用?

user-controls - 开发期间的用户控制位置和更新

c# - 将一种类型的委托(delegate)动态转换为另一种

c# - 从类型名称和程序集名称创建实例

android - 如何转换 Android 位图以包裹圆柱体并改变透视图

c# - 如何部署对 cs 文件 (ascx.cs) 进行更改的单个用户控件

wpf - 将用户控件扩展到完整的窗口大小