안녕하세요
먼저 콤보박스를 상속받는 클래스를 하나 작성하시고..
//드로우 모드를 OwnerDrawVariable로 변경
DrawMode = DrawMode.OwnerDrawVariable;
OnMeasureItem , OnDrawItem 두개의 메서드를 재정의 하시면 되겄습니다.
한번 짜봤습니다.
/// <summary>
/// ImageCombo
/// </summary>
public class ImageCombo: ComboBox
{
ImageList imageList = null;
public ImageList Images
{
get { return this.imageList; }
set { this.imageList = value; }
}
public ImageCombo()
{
DrawMode = DrawMode.OwnerDrawVariable;
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
base.OnMeasureItem (e);
e.ItemWidth = DropDownWidth + 24;
}
/// <summary>
/// OnDrawItem 메서드의 오버라이드 입니다.
/// </summary>
/// <param name="e"></param>
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
base.OnDrawItem(e);
if(e.Index > -1)
{
string text = Items[e.Index].ToString();
Image img = null;
if(this.imageList != null)
{
if(this.imageList.Images.Count > e.Index)
{
img = imageList.Images[e.Index];
}
}
//선택한 상황이 아니라면
if((e.State & DrawItemState.Focus)==0)
{
using(SolidBrush backBrush = new SolidBrush(SystemColors.Window))
{
e.Graphics.FillRectangle(backBrush,
e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);
}
if(img != null)
e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, 24, e.Bounds.Height);
using(SolidBrush brush = new SolidBrush(SystemColors.WindowText))
{
e.Graphics.DrawString(text, this.Font, brush, e.Bounds.X + 24,e.Bounds.Y);
}
}
//선택한 상황이라면
else
{
using(SolidBrush backBrush = new SolidBrush(SystemColors.Highlight))
{
e.Graphics.FillRectangle(backBrush,
e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);
}
if(img != null)
e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, 24, e.Bounds.Height);
using(SolidBrush brush = new SolidBrush(SystemColors.HighlightText))
{
e.Graphics.DrawString(text, this.Font, brush, e.Bounds.X + 24,e.Bounds.Y);
}
}
}
}
}
사용하실때는 디자인 모드에서 이미지 리스트 하나 생성 하시고
콤보박스의 Images에다가 지정 하시면 됩니다.
출처: 데브피아