为了让程序更具个性,有时候我们需要在ComboBox中绘制图形。
作者:Zealot
为了让程序更具个性,有时候我们需要在ComboBox中绘制图形。效果如图所示。
![DrawComboBoxItem2.JPG](https://i-blog.csdnimg.cn/blog_migrate/b50a6f9847efbb44a8adf4375be7d318.jpeg)
![DrawComboBoxItem3.JPG](https://i-blog.csdnimg.cn/blog_migrate/d56d0f98c0bd3fc3bb40e61d83f1b9bb.jpeg)
代码如下:
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;
![None.gif](/Images/OutliningIndicators/None.gif)
partial
class
Form2 : Form
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public
Form2()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
"
);
![InBlock.gif](/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);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
void
comboBox1_MeasureItem(
object
sender, MeasureItemEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private
String RemoveSpaces(String str1)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int
start;
int
at;
int
end;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
String str2
=
String.Copy(str1);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
at
=
0
;
end
=
str2.Length
-
1
;
start
=
0
;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
while
((start
<=
end)
&&
(at
>
-
1
))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
start+count must be a position within str2.
at
=
str2.IndexOf(
"
"
, start);
if
(at
==
-
1
)
break
;
str2
=
str2.Remove(at,
1
);
start
=
at
+
1
;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
return
str2;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private
HatchStyle getHatchStyle(String s)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
String str
=
RemoveSpaces(s);
return
(HatchStyle)Enum.Parse(
typeof
(HatchStyle), str,
true
);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
void
comboBox1_DrawItem(
object
sender, DrawItemEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
throw new NotImplementedException();
e.DrawBackground();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Rectangle r
=
e.Bounds;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
if
(e.Index
!=
-
1
)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if
(e.Index
>
0
)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Rectangle rd
=
r;
rd.Width
=
rd.Left
+
25
;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Rectangle rt
=
r;
r.X
=
rd.Right;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
string
displayText
=
this
.comboBox1.Items[e.Index].ToString();
HatchStyle hs
=
this
.getHatchStyle(displayText);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
using
(HatchBrush b
=
new
HatchBrush(hs,e.ForeColor,e.BackColor))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
e.Graphics.FillRectangle(b,rd);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
StringFormat sf
=
new
StringFormat();
sf.Alignment
=
StringAlignment.Near;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
using
(SolidBrush sb
=
new
SolidBrush(Color.White))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if
((e.State
&
DrawItemState.Focus)
==
0
)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Color
=
SystemColors.Window;
e.Graphics.FillRectangle(sb,r);
sb.Color
=
SystemColors.WindowText;
e.Graphics.DrawString(displayText,
this
.Font,sb,r,sf);
}
else
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
using
(SolidBrush sb
=
new
SolidBrush(Color.White))
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if
((e.State
&
DrawItemState.Focus)
==
0
)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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();
}
}