需要引用命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
1.首先需要写一个ITEM的类用来保存列表中的项(ITEM)
/// <summary>
/// IMAGECOMBOBOX项
/// </summary>
public class ImageComboBoxItem : object
{
#region 成员变量
private Color foreColor = Color.FromKnownColor(KnownColor.Transparent);
private bool bold = false;
private int imageIndex = -1;
private object tag = null;
private string text = null;
#endregion
#region 构造函数
public ImageComboBoxItem()
{}
public ImageComboBoxItem(string Text)
{
text = Text;
}
public ImageComboBoxItem(string Text,int ImageIndex)
: this(Text)
{
imageIndex = ImageIndex;
}
public ImageComboBoxItem(string Text,int ImageIndex,bool Bold)
: this(Text,ImageIndex)
{
bold = Bold;
}
public ImageComboBoxItem(string Text,int ImageIndex,bool Bold,Color ForeColor)
: this(Text,ImageIndex,Bold)
{
foreColor = ForeColor;
}
public ImageComboBoxItem(string Text,int ImageIndex,bool Bold,Color ForeColor,object Tag)
: this(Text,ImageIndex,Bold,ForeColor)
{
tag = Tag;
}
#endregion
#region 属性
/// <summary>
/// 文本颜色
/// </summary>
public Color ForeColor
{
get {return foreColor;}
set {foreColor = value;}
}
/// <summary>
/// 字体是否加粗
/// </summary>
public bool Bold
{
get {return bold;}
set {bold = value;}
}
/// <summary>
/// 附加数据
/// </summary>
public object Tag
{
get {return tag;}
set {tag = value;}
}
/// <summary>
/// 图标索引[C#]WINFORM控件—带图标的COMBOBOX
/// </summary>
public int ImageIndex
{
get {return imageIndex;}
set {imageIndex = value;}
}
/// <summary>
/// 项目文本
/// </summary>
public string Text
{
get {return text;}
set {text = value;}
}
#endregion
public override string ToString()
{
return text;
}
}
2.写COMBOBOX的派生类,重写ITEM的绘制过程
/// <summary>
/// 带图标的COMBOX控件
/// </summary>
public class ImageComboBox : ComboBox
{
private ImageList imgs = new ImageList();//图片列表
#region 构造函数
public ImageComboBox()
{
//设置绘制模式
this.DrawMode = DrawMode.OwnerDrawFixed;
}
#endregion
#region 属性
public ImageList ImageList
{
get {return imgs;}
set {imgs = value;}
}
#endregion
/// <summary>
/// 重写绘制ITEM的过程
/// </summary>
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if(e.Index < 0)
e.Graphics.DrawString(this.Text,e.Font,
new SolidBrush(e.ForeColor),e.Bounds.Left+imgs.ImageSize.Width,e.Bounds.Top);
else
{
//是否ImageComboBoxItem
if(this.Items[e.Index].GetType()==typeof(ImageComboBoxItem))
{
ImageComboBoxItem item = (ImageComboBoxItem)this.Items[e.Index];
//获取颜色和字体
Color foreColor = (item.ForeColor!=Color.FromKnownColor(KnownColor.Transparent))?item.ForeColor:e.ForeColor;
Font font = item.Bold?(new Font(e.Font,FontStyle.Bold)):e.Font;
// -1:没有图标
if(item.ImageIndex != -1)
{
//画图标和文本
this.ImageList.Draw(e.Graphics, e.Bounds.Left, e.Bounds.Top, item.ImageIndex);
e.Graphics.DrawString(item.Text, font, new SolidBrush(foreColor), e.Bounds.Left+imgs.ImageSize.Width, e.Bounds.Top);
}
else//画文本
e.Graphics.DrawString(item.Text, font, new SolidBrush(foreColor), e.Bounds.Left+imgs.ImageSize.Width, e.Bounds.Top);
}
else
e.Graphics.DrawString(this.Items[e.Index].ToString(),e.Font,
new SolidBrush(e.ForeColor),e.Bounds.Left+imgs.ImageSize.Width,e.Bounds.Top);
}
base.OnDrawItem (e);[C#]WINFORM控件—带图标的COMBOBOX
}