C#中怎么实现在combobox显示图标 如题哪位大侠指点指点!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 列表框COMbobox没有backimag属性.添加图片难道需要自己写方法吗? 无论是下拉还是选中,都需要重新绘制,也就是就combobox本身及图象,都是重新绘制上去的. this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { e.Graphics.DrawImage(this.imageList1.Images[e.Index], e.Bounds.X, e.Bounds.Y, 32, e.Bounds.Height); if (e.State == DrawItemState.Selected) { Pen p = new Pen(Color.White); Pen b=new Pen(SystemColors.Highlight); e.Graphics.FillRectangle(b.Brush, e.Bounds.X + 32, e.Bounds.Y, e.Bounds.Width - 32, e.Bounds.Height); e.Graphics.DrawString(this.comboBox1.Items[e.Index].ToString(), this.Font, p.Brush, e.Bounds.X + 35, e.Bounds.Y); } else { Pen p = new Pen(Color.Black); Pen b = new Pen(Color.White); e.Graphics.FillRectangle(b.Brush, e.Bounds.X + 32, e.Bounds.Y, e.Bounds.Width - 32, e.Bounds.Height); e.Graphics.DrawString(this.comboBox1.Items[e.Index].ToString(), this.Font, p.Brush, e.Bounds.X + 35, e.Bounds.Y); } } 首先在窗体中添加一个comboBox1和一个imageList(本例中带有三个图片)private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { Graphics g = e.Graphics; Rectangle r = e.Bounds; Size imageSize = imageList1.ImageSize; if (e.Index >= 0) { Font fn = new Font("Tahoma", 10, FontStyle.Bold); string s = (string)comboBox1.Items[e.Index]; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { //画条目背景 e.Graphics.FillRectangle(new SolidBrush(Color.Red), r); //绘制图像 imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index); //显示字符串 e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top); //显示取得焦点时的虚线框 e.DrawFocusRectangle(); } else { e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r); imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index); e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top); e.DrawFocusRectangle(); } } } private void Form1_Load(object sender, EventArgs e) { comboBox1.DrawMode = DrawMode.OwnerDrawFixed; comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; comboBox1.Items.Add("小车"); comboBox1.Items.Add("视频"); comboBox1.Items.Add("信号灯"); } 首先在窗体中添加一个comboBox1和一个imageList(本例中带有三个图片) private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { Graphics g = e.Graphics; Rectangle r = e.Bounds; Size imageSize = imageList1.ImageSize; if (e.Index >= 0) { Font fn = new Font("Tahoma", 10, FontStyle.Bold); string s = (string)comboBox1.Items[e.Index]; StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Near; if (e.State == (DrawItemState.NoAccelerator ¦ DrawItemState.NoFocusRect)) { //画条目背景 e.Graphics.FillRectangle(new SolidBrush(Color.Red), r); //绘制图像 imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index); //显示字符串 e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top); //显示取得焦点时的虚线框 e.DrawFocusRectangle(); } else { e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r); imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index); e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top); e.DrawFocusRectangle(); } } } private void Form1_Load(object sender, EventArgs e) { comboBox1.DrawMode = DrawMode.OwnerDrawFixed; comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; comboBox1.Items.Add("小车"); comboBox1.Items.Add("视频"); comboBox1.Items.Add("信号灯"); } 没有吧 vs2005开始combox本身就支持显示图片的 DateGridView最后一行与底边的空隙怎么取消掉 求助 抽奖小程序 C#随机产生选择题(winform) 图片大小问题 用UDP做QQ的聊天信息传输该怎么写?可否完全离开数据库? 关于Treeview的小问题 又有问题了。 按钮事件问题 winfrom打印的问题 net remoting 的问题,请高手做答(在线,急) 如何遍历tabcontrol控件的所有的tabpage中的所有控件? 高手请谈谈VS2005自带的TableAdapter?
{
e.Graphics.DrawImage(this.imageList1.Images[e.Index], e.Bounds.X, e.Bounds.Y, 32, e.Bounds.Height);
if (e.State == DrawItemState.Selected)
{
Pen p = new Pen(Color.White);
Pen b=new Pen(SystemColors.Highlight);
e.Graphics.FillRectangle(b.Brush, e.Bounds.X + 32, e.Bounds.Y, e.Bounds.Width - 32, e.Bounds.Height);
e.Graphics.DrawString(this.comboBox1.Items[e.Index].ToString(), this.Font, p.Brush, e.Bounds.X + 35, e.Bounds.Y);
}
else
{
Pen p = new Pen(Color.Black);
Pen b = new Pen(Color.White);
e.Graphics.FillRectangle(b.Brush, e.Bounds.X + 32, e.Bounds.Y, e.Bounds.Width - 32, e.Bounds.Height);
e.Graphics.DrawString(this.comboBox1.Items[e.Index].ToString(), this.Font, p.Brush, e.Bounds.X + 35, e.Bounds.Y);
}
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
Size imageSize = imageList1.ImageSize;
if (e.Index >= 0)
{
Font fn = new Font("Tahoma", 10, FontStyle.Bold);
string s = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
{
//画条目背景
e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
//绘制图像
imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
//显示字符串
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
//显示取得焦点时的虚线框
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
e.DrawFocusRectangle();
}
} } private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.Add("小车");
comboBox1.Items.Add("视频");
comboBox1.Items.Add("信号灯");
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle r = e.Bounds;
Size imageSize = imageList1.ImageSize;
if (e.Index >= 0)
{
Font fn = new Font("Tahoma", 10, FontStyle.Bold);
string s = (string)comboBox1.Items[e.Index];
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
if (e.State == (DrawItemState.NoAccelerator ¦ DrawItemState.NoFocusRect))
{
//画条目背景
e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
//绘制图像
imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
//显示字符串
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
//显示取得焦点时的虚线框
e.DrawFocusRectangle();
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
e.DrawFocusRectangle();
}
} } private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.Add("小车");
comboBox1.Items.Add("视频");
comboBox1.Items.Add("信号灯"); }