c#給用戶自定義控件添加圖片
自己定製的用戶控件總是一個樣,太難看了,能不能定製自己控件的圖標呢?答案是:yes
自己在網上搜一下滿足了自己的想法:
大家可以到上面的地址去看一下。
先給出我做的效果圖:
下面給出一下具體的步驟。
-
新建一個用戶控件
-
向資源文件是添加一張圖片,圖片格式可以是bm,ico 大小最好是 16 * 16 啦!
-
選中用戶控件圖標,單擊“右鍵》屬性”把 “生成操作 的屬性值改爲:嵌入的資源(Action Resource)
-
第四步
-
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace Mycontrol
{
/// <summary>
/// 這裏是一個鍵
/// Mycontrol爲定義這個控件的類名
/// Resources 文件夾名vb6tovb7.bmp圖片名稱
/// </summary>
[ToolboxBitmap(typeof(MyControl), "Resources.vb6tovb7.bmp")]
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
}
}
關鍵在添加這一句:[ToolboxBitmap(typeof(MyControl), "Resources.vb6tovb7.bmp")]
這樣就實現了這個功能。