在ComboBox中绘制图形
为了让程序更具个性,有时候我们需要在ComboBox中绘制图形。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
![](/Images/OutliningIndicators/None.gif)
partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.comboBox1.Items.Add("Solid Brush");
this.comboBox1.Items.Add("Horizontal");
this.comboBox1.Items.Add("Min");
this.comboBox1.Items.Add("Vertical");
this.comboBox1.Items.Add("Forward Diagonal");
this.comboBox1.Items.Add("Backward Diagonal");
this.comboBox1.Items.Add("Cross");
this.comboBox1.Items.Add("Large Grid");
this.comboBox1.Items.Add("Max");
this.comboBox1.Items.Add("Diagonal Cross");
![](/Images/OutliningIndicators/InBlock.gif)
this.comboBox1.SelectedIndex = 0;
this.comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox1.DropDownWidth = 190;
this.comboBox1.DropDownHeight = 300;
this.comboBox1.MeasureItem += new MeasureItemEventHandler(comboBox1_MeasureItem);
this.comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
}
![](/Images/OutliningIndicators/InBlock.gif)
void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
string displayText = this.comboBox1.Items[e.Index].ToString();
SizeF stringSize = e.Graphics.MeasureString(displayText, this.Font);
e.ItemHeight = (int)stringSize.Height;
e.ItemWidth = (int)stringSize.Width;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private String RemoveSpaces(String str1)
{
int start;
int at;
int end;
![](/Images/OutliningIndicators/InBlock.gif)
String str2 = String.Copy(str1);
![](/Images/OutliningIndicators/InBlock.gif)
at = 0;
end = str2.Length - 1;
start = 0;
![](/Images/OutliningIndicators/InBlock.gif)
while ((start <= end) && (at > -1))
{
// start+count must be a position within str2.
at = str2.IndexOf(" ", start);
if (at == -1) break;
str2 = str2.Remove(at, 1);
start = at + 1;
}
![](/Images/OutliningIndicators/InBlock.gif)
return str2;
}
![](/Images/OutliningIndicators/InBlock.gif)
private HatchStyle getHatchStyle(String s)
{
String str = RemoveSpaces(s);
return (HatchStyle)Enum.Parse(typeof(HatchStyle), str, true);
}
![](/Images/OutliningIndicators/InBlock.gif)
void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//throw new NotImplementedException();
e.DrawBackground();
![](/Images/OutliningIndicators/InBlock.gif)
Rectangle r = e.Bounds;
![](/Images/OutliningIndicators/InBlock.gif)
if (e.Index != -1)
{
if (e.Index > 0)
{
Rectangle rd = r;
rd.Width = rd.Left + 25;
![](/Images/OutliningIndicators/InBlock.gif)
Rectangle rt = r;
r.X = rd.Right;
![](/Images/OutliningIndicators/InBlock.gif)
string displayText = this.comboBox1.Items[e.Index].ToString();
HatchStyle hs = this.getHatchStyle(displayText);
![](/Images/OutliningIndicators/InBlock.gif)
using (HatchBrush b = new HatchBrush(hs,e.ForeColor,e.BackColor))
{
e.Graphics.FillRectangle(b,rd);
}
![](/Images/OutliningIndicators/InBlock.gif)
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
![](/Images/OutliningIndicators/InBlock.gif)
using(SolidBrush sb = new SolidBrush(Color.White))
{
if((e.State & DrawItemState.Focus) == 0)
{
sb.Color = SystemColors.Window;
e.Graphics.FillRectangle(sb,r);
sb.Color = SystemColors.WindowText;
e.Graphics.DrawString(displayText,this.Font,sb,r,sf);
}
else
{
sb.Color = SystemColors.Highlight;
e.Graphics.FillRectangle(sb,r);
sb.Color = SystemColors.HighlightText;
e.Graphics.DrawString(displayText,this.Font,sb,r,sf);
}
}
} // if (e.Index > 0)
else
{
using(SolidBrush sb = new SolidBrush(Color.White))
{
if((e.State & DrawItemState.Focus) == 0)
{
sb.Color = SystemColors.Window;
e.Graphics.FillRectangle(sb,e.Bounds);
string displayText = this.comboBox1.Items[e.Index].ToString();
sb.Color = SystemColors.WindowText;
e.Graphics.DrawString(displayText,this.Font,sb,e.Bounds);
}
else
{
sb.Color = SystemColors.Highlight;
e.Graphics.FillRectangle(sb,e.Bounds);
string displayText = this.comboBox1.Items[e.Index].ToString();
sb.Color = SystemColors.HighlightText;
e.Graphics.DrawString(displayText,this.Font,sb,e.Bounds);
}
}
}
e.DrawFocusRectangle();
}
}
作者:Zealot
为了让程序更具个性,有时候我们需要在ComboBox中绘制图形。效果如图所示。
代码如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)