C# 實現顏色下拉列表框

代碼如下:

class ColorDropDownList : ComboBox 
{
	public ColorDropDownList() : base() 
	{
		this.DropDownStyle = ComboBoxStyle.DropDownList;
		this.DrawMode = DrawMode.OwnerDrawFixed;
		fillList();
		this.SelectedIndex = 0;
		this.DrawItem += new DrawItemEventHandler(ColorDropDownList_DrawItem);
	}
	private void fillList() 
	{
		string[] names = Enum.GetNames(typeof(KnownColor));
		this.Items.AddRange(names);
	}
	private void ColorDropDownList_DrawItem(object sender, DrawItemEventArgs e) 
	{
		string colorName = (string)this.Items[e.Index];
		Color color = Color.FromName(colorName);
		Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width / 4, e.Bounds.Height - 2);
		Brush brush = new SolidBrush(color);
		e.Graphics.DrawRectangle(new Pen(Color.Black), rect);
		rect.X += 1;
		rect.Y += 1;
		rect.Width -= 1;
		rect.Height -= 1;
		e.Graphics.FillRectangle(brush, rect);
		rect.Offset(rect.Width + 4, 0);
		e.Graphics.DrawString(colorName, e.Font, Brushes.Black, rect.Location);
	}
}

 

效果圖如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章