如何将imge设置为combox listicon [英] how to set imge to combox listicon

查看:78
本文介绍了如何将imge设置为combox listicon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想为组合框箭头图标添加不同的图像和颜色,所以我能做什么,我尝试了这个特定的代码,但问题没有解决



Mycode :



公共密封类ColorSelector:ComboBox

{

public ColorSelector()

$

DrawMode = DrawMode.OwnerDrawFixed;

DropDownStyle = ComboBoxStyle.DropDownList;

}



protected override void OnDrawItem(DrawItemEventArgs e)

{

e.DrawBackground();



e.DrawFocusRectangle();



if(e.Index> = 0 && e.Index< Items.Count)

{

DropDownItem item =(DropDownItem)Items [e.Index];



e.Graphics.DrawImage(item.I mage,e.Bounds.Left,e.Bounds.Top);



e.Graphics.DrawString(item.Value,e.Font,new SolidBrush(e。 ForeColor),e.Bounds.Left + item.Image.Width,e.Bounds.Top + 2);

}



base .OnDrawItem(e);

}

}

公共密封类DropDownItem

{

public string Value {get;组; }



public Image Image {get;组; }



公共DropDownItem()

:这个()

{}



public DropDownItem(string val)

{

Value = val;

Image = new Bitmap (16,16);

使用(Graphics g = Graphics.FromImage(Image))

{

using(Brush b = new SolidBrush) (Color.FromName(val)))

{

g.DrawRectangle(Pens.White,0,0,Image.Width,Image.Height);

g.FillRectangle(b,1,1,Image.Width - 1,Image.Height - 1);

}

}

}



公共覆盖字符串ToString()

{

返回值;

}

}

解决方案

快速浏览后我会说你不应该打电话给

 base.OnDrawItem(e); 


actually i want to add different image and color to combobox arrow icon,so what can i do ,i was tried this specific code but problem not solved

Mycode:

public sealed class ColorSelector : ComboBox
{
public ColorSelector()
{
DrawMode = DrawMode.OwnerDrawFixed;
DropDownStyle = ComboBoxStyle.DropDownList;
}

protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();

e.DrawFocusRectangle();

if (e.Index >= 0 && e.Index < Items.Count)
{
DropDownItem item = (DropDownItem)Items[e.Index];

e.Graphics.DrawImage(item.Image, e.Bounds.Left, e.Bounds.Top);

e.Graphics.DrawString(item.Value, e.Font, new SolidBrush(e.ForeColor), e.Bounds.Left + item.Image.Width, e.Bounds.Top + 2);
}

base.OnDrawItem(e);
}
}
public sealed class DropDownItem
{
public string Value { get; set; }

public Image Image { get; set; }

public DropDownItem()
: this("")
{ }

public DropDownItem(string val)
{
Value = val;
Image = new Bitmap(16, 16);
using (Graphics g = Graphics.FromImage(Image))
{
using (Brush b = new SolidBrush(Color.FromName(val)))
{
g.DrawRectangle(Pens.White, 0, 0, Image.Width, Image.Height);
g.FillRectangle(b, 1, 1, Image.Width - 1, Image.Height - 1);
}
}
}

public override string ToString()
{
return Value;
}
}

解决方案

After a quick look I'd say you should not call

base.OnDrawItem(e);


这篇关于如何将imge设置为combox listicon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆