.NET 1.0Visual Studio .NET 2003Windows 2003.NET 1.1Windows 2000Windows XP中级开发Visual StudioWindows.NETC#
.NET 系统画笔绘制器






4.85/5 (16投票s)
2004年3月10日
1分钟阅读

62693

692
展示了如何使用不同的画刷并编写所有者绘制的列表框
引言
系统画笔绘制器是一个小型实用工具,用于查看 .NET 框架系统定义的填充图案,来自 System.Drawing.Drawing2D.HatchStyle
,以及 Windows 系统颜色和命名颜色,来自 Color
结构。
示例展示了如何使用不同的画笔以及如何通过所有者绘制代码扩展 ListBox
。
绘制器
该代码使用了 2 种不同的绘制器(并且可以轻松创建新的绘制器)。所有绘制器都实现了 IPainter
接口。该接口的作用是获取将在列表框中显示的名称以及用于填充的 Brush
。
public interface IPainter
{
Brush PaintBrush
{
get;
}
string Name
{
get;
}
}
我们还有一个用于可用绘制器的实用枚举。
public enum PaintType
{
Hatches, // draw hatches from HatchStyle
SystemColors, // draw system colors
Colors // draw NamedColors (except system colors)
}
第一个绘制器是颜色绘制器。它所做的就是用纯色填充列表框项。
public class ColorPainter : IPainter
{
Color color;
public ColorPainter(Color color)
{
this.color = color;
}
public string Name
{
get { return color.ToKnownColor().ToString(); }
}
public Brush PaintBrush
{
get { return new SolidBrush(color); }
}
}
另一个是 Hatch
绘制器,它使用填充画笔。
public class HatchPainter : IPainter
{
HatchStyle style;
Color color1; // foreground color for hatch
Color color2; // background color for hatch
public HatchPainter(HatchStyle style, Color color1, Color color2)
{
this.style = style;
this.color1 = color1;
this.color2 = color2;
}
/// <SUMMARY>
/// for name just return hatch enum name
/// </SUMMARY>
public string Name
{
get { return style.ToString(); }
}
/// <SUMMARY>
/// return HatchBrush with current style and colors
/// </SUMMARY>
public Brush PaintBrush
{
get {
return new HatchBrush(style, color1, color2);
}
}
....
这就是绘制器的全部。我们有 3 种不同的类型,只有 2 个绘制器,因为 SystemColor
s 和 Colors 使用相同的绘制器。
ListBox
列表框的实现是一个所有者绘制的列表框。为此,我们所要做的就是
this.DrawMode = DrawMode.OwnerDrawFixed;
在构造函数中。我们还在列表框上有一个自定义的 PaintType
属性,该属性决定使用哪种类型的绘制器 public PaintType PaintType
{
get { return this.paintType; }
set
{
paintType = value;
InitValues();
}
}
当我们分配此属性时,我们会初始化列表中的项目。
void InitValues()
{
this.Items.Clear();
if (paintType == PaintType.Hatches)
{
// for hatches we get all values from HatchStyle enumeration
foreach (HatchStyle style in Enum.GetValues(typeof(HatchStyle)))
{
IPainter painter = new HatchPainter(style,
Color.Blue, Color.Transparent);
Items.Add(painter);
}
}
else if (paintType == PaintType.Colors)
{
// for colors we get all color values from KnownColor
// and skip system colors
foreach (KnownColor kcolor in Enum.GetValues(typeof(KnownColor)))
{
Color color = Color.FromKnownColor(kcolor);
if (!color.IsSystemColor)
{
IPainter painter = new ColorPainter(color);
Items.Add(painter);
}
}
}
else if (paintType == PaintType.SystemColors)
{
// for systemcolors we get all known colors
// and only use system colors
foreach (KnownColor kcolor in Enum.GetValues(typeof(KnownColor)))
{
Color color = Color.FromKnownColor(kcolor);
if (color.IsSystemColor)
{
IPainter painter = new ColorPainter(color);
Items.Add(painter);
}
}
}
}
根据我们想要使用的绘制器类型,我们用 IPainter
值填充列表。稍后,当我们绘制项目时,我们不再关心列表的类型,因为 IPainter
会告诉我们如何绘制它。最后是绘制项目代码
// each item in the list IPainter
IPainter painter = (IPainter)Items[e.Index];
// get brush from painter
Brush brush = painter.PaintBrush;
// fill the item with painters brush
g.FillRectangle(brush, e.Bounds);
g.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Bottom-1,
e.Bounds.Right, e.Bounds.Bottom-1);
// draw box with painter name
string name = painter.Name;
int width = (int)g.MeasureString(name, Font).Width;
g.FillRectangle((e.State & DrawItemState.Selected) ==
DrawItemState.Selected ? Brushes.Yellow : Brushes.White,
3, e.Bounds.Top+3, width+3, Font.Height+5);
g.DrawRectangle(Pens.Black, 3, e.Bounds.Top+3, width+3, Font.Height+5);
g.DrawString(painter.Name, Font, Brushes.Black, 5, e.Bounds.Top+5);
brush.Dispose();
我们用绘制器的画笔填充区域,在每个项目下方绘制一条线来分隔它们,并绘制一个带有绘制器名称的框。