ComBox显示图像,结合ImageList的Draw方法:
combox属性设置:
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
DrawItem方法中进行实现:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics r = e.Graphics;
Rectangle rec = e.Bounds;
Size imageSize = imageList1.ImageSize;
if (e.Index >=0)
{ //设置文本的方式
string s = comboBox1.Items[e.Index].ToString();
StringFormat SF = new StringFormat();
SF.Alignment = StringAlignment.Near;
//如果当前项没有焦点、键盘加速键
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
imageList1.Draw(e.Graphics, rec.Left, rec.Top, e.Index);
e.Graphics.DrawString(s, new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Black), rec.Left + imageSize.Width, rec.Top);
e.DrawFocusRectangle(); //取得焦点时候的虚线框
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.White), rec); //画条目背景
imageList1.Draw(e.Graphics, rec.Left, rec.Top, e.Index);
e.Graphics.DrawString(s, new Font("宋体", 10, FontStyle.Bold), new SolidBrush(Color.Black), rec.Left + imageSize.Width, rec.Top);
e.DrawFocusRectangle();
}
}
}