【问题标题】:Creating a custom toolbox icon for my custom user control为我的自定义用户控件创建自定义工具箱图标
【发布时间】:2015-02-15 07:19:22
【问题描述】:

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

我有一个类似于以下布局的解决方案...

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 表单上使用类似蓝色的小齿轮作为工具箱图标时。


来源:

【问题讨论】:

    标签: c# bitmap user-controls


    【解决方案1】:

    看来答案很简单……其实我什么都做对了!

    不过有一点值得注意……

    如果您使用参考,自定义工具箱图标将不会显示 到同一解决方案中的项目。

    要修复/解决此问题,您需要执行以下操作...

    添加对已编译(自定义控件的产物)的引用 project) DLL 文件。

    我最终在编译后删除了自定义控件项目,然后添加了对我想要使用它的项目的引用。完成后,我右键单击工具箱,然后单击“选择项目...”。然后单击浏览并从那里加载 DLL。

    现在自定义工具箱图标显示得很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-10
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多