继承的应用-为ComboBox控件添加图片
为ComboBox控件添加图片 .NET自带的ComboBox控件没有添加图片的功能,在我们学习了继承以后,我们可以继承原有的ComboBox的功能并且加入扩展的新功能。
下面我们对ComboBox进行扩展,实现在ComboBox中显示图片的功能: 1.添加类 ComboBoxExItem:
class ComboBoxExItem
{
private string _text;
public string Text
{
get { return _text; }
set { _text = value; }
}
private int _imageIndex;
public int ImageIndex
{
get { return _imageIndex; }
set { _imageIndex = value; }
}
public ComboBoxExItem()
: this("")
{
}
public ComboBoxExItem(string text)
: this(text, -1)
{
}
public ComboBoxExItem(string text, int imageIndex)
{
_text = text;
_imageIndex = imageIndex;
}
public override string ToString()
{
return _text;
}
}
2.添加类:ComboBoxEx
class ComboBoxEx : ComboBox
{
private ImageList imageList;
public ImageList ImageList
{
get { return imageList; }
set { imageList = value; }
}
public ComboBoxEx()
{
DrawMode = DrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs ea)
{
ea.DrawBackground();
ea.DrawFocusRectangle();
ComboBoxExItem item;
Size imageSize = imageList.ImageSize;
Rectangle bounds = ea.Bounds;
try
{
item = (ComboBoxExItem)Items[ea.Index];
if (item.ImageIndex != -1)
{
imageList.Draw(ea.Graphics, bounds.Left,
bounds.Top,
item.ImageIndex);
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left + imageSize.Width, bounds.Top);
}
else
{
ea.Graphics.DrawString(item.Text, ea.Font, new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
catch
{
if (ea.Index != -1)
{
ea.Graphics.DrawString(Items[ea.Index].ToString(), ea.Font,
new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
else
{
ea.Graphics.DrawString(Text, ea.Font,
new
SolidBrush(ea.ForeColor), bounds.Left, bounds.Top);
}
}
base.OnDrawItem(ea);
}
}
当上述两个步骤完成之后,我们就可以使用ComboBoxEx来代替ComboBox来实现带图片的ComboBox了
以下是应用的代码:(窗体的加载代码)
private void InitializeComponent()
{
this.components = new
System.ComponentModel.Container();
ComboBoxExItem comboBoxExItem1 = new ComboBoxExItem();
ComboBoxExItem comboBoxExItem2 = new ComboBoxExItem();
ComboBoxExItem comboBoxExItem3 = new ComboBoxExItem();
System.ComponentModel.ComponentResourceManager resources = new
System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.comboBox1 = new ComboBoxEx();
this.imageList1 = new
System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.comboBox1.ImageList = this.imageList1;
comboBoxExItem1.ImageIndex = 0;
comboBoxExItem1.Text = "【IT精彩世界】";
comboBoxExItem2.ImageIndex = 1;
comboBoxExItem2.Text = "【IT精彩世界】";
comboBoxExItem3.ImageIndex = 2;
comboBoxExItem3.Text = "【IT精彩世界】";
this.comboBox1.Items.AddRange(new object[] {
comboBoxExItem1,
comboBoxExItem2,
comboBoxExItem3});
this.comboBox1.Location = new
System.Drawing.Point(48, 109);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new
System.Drawing.Size(200, 22);
this.comboBox1.TabIndex = 0;
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.imageList1.Images.SetKeyName(0, "welcome.JPG");
this.imageList1.Images.SetKeyName(1, "welcome.JPG");
this.imageList1.Images.SetKeyName(2, "welcome.JPG");
//
// Form1
//
this.AutoScaleBaseSize = new
System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}