作者:Werdna               翻译:小新0574

原文及代码链接:http://www.codeproject.com/cs/media/SystemPainter.asp

Introduction

System painter is a little utility for viewing .NET framework system defined hatches from System.Drawing.Drawing2D.HatchStyle and windows system colors and named colors from Color structure.

The example shows how to use different brushes and how to extend ListBox with owner drawn code.

System painter是一个用来查看.NET frameworkSystem.Drawing.Drawing2D.HatchStyle里的系统预定义的hatcheswindows系统颜色以及Color结构里的颜色的一个小工具。

Painters

The code uses 2 different painters (with easy ability to create new different ones). All painters implement IPainter interface. All the interface does it to get the name that will show in list box and Brush used for filling.

代码使用了2种不同的painters(同时很方便创建新的painters)。所有的painters实现了IPainter接口。所有的界面使用它来得到要显示在list box里的名字和用来填充的Brush

CodeProject - .NET System Brushes Painter    public interface IPainter

We also have utility enumeration for available painters:

我们还有为可用painters准备的工具枚举:

CodeProject - .NET System Brushes Painter    public enum PaintType

First painter is the color painter. All it does is filling the listbox item with solid color:

第一个painter是一个颜色painter。它所做的就是用纯色填充listbox

CodeProject - .NET System Brushes Painter public class ColorPainter : IPainter

And another one is Hatch painter that uses hatch brush:

另一个是使用hatch刷子的Hatch painter

CodeProject - .NET System Brushes Painter public class HatchPainter : IPainter

That's it for painters. We have 3 different types and only 2 painters because SystemColors and Colors use the same painter.

这就是为painters所做的了。我们有3个不同的类型,但只有两个painters,因为SystemColorsColors使用同样的painter

ListBox

The implementation of the listbox is an owner drawn listbox. To do that all we have to do is:
 

Listbox实现的是一个自绘的listbox。为了做这些,我们所要做的是在构造器里写上这个:

CodeProject - .NET System Brushes Painter  this.DrawMode = DrawMode.OwnerDrawFixed;

in constructor. We also have custom PaintType property on the listbox that decides what type of painter to use:

 

我们还有自定义的PaintType属性作用于listbox,这个属性决定了使用哪种类型的painter

CodeProject - .NET System Brushes Painter public PaintType PaintType

when we assign this property we initialize the items in the list:

当我们指派这个属性时,我们在列表里初始化项:

CodeProject - .NET System Brushes Paintervoid InitValues()


Depending on the type of painter that we want to use, we populate the list with IPainter values. Later when we draw the items we don't care about what type of list it is, because IPainter will tell us how to paint it. And finally the draw item code:

根据我们想用的painter类型,我们在列表里使用了IPainter 值。以后当我们绘制项时,我们不用介意list是什么类型了,因为IPainter会告诉我们怎么绘制它。最后的绘制项的代码:

CodeProject - .NET System Brushes Painter// each item in the list IPainter
CodeProject - .NET System Brushes Painter
IPainter painter = (IPainter)Items[e.Index];
CodeProject - .NET System Brushes Painter
CodeProject - .NET System Brushes Painter
// get brush from painter
CodeProject - .NET System Brushes Painter
Brush brush = painter.PaintBrush;
CodeProject - .NET System Brushes Painter
CodeProject - .NET System Brushes Painter
// fill the item with painters brush
CodeProject - .NET System Brushes Painter
g.FillRectangle(brush, e.Bounds);
CodeProject - .NET System Brushes Painterg.DrawLine(Pens.Black, e.Bounds.X, e.Bounds.Bottom
-1
CodeProject - .NET System Brushes Painter  e.Bounds.Right, e.Bounds.Bottom
-1);
CodeProject - .NET System Brushes Painter
CodeProject - .NET System Brushes Painter
// draw box with painter name
CodeProject - .NET System Brushes Painter
string name = painter.Name;
CodeProject - .NET System Brushes Painter
int width = (int)g.MeasureString(name, Font).Width;
CodeProject - .NET System Brushes Painter
CodeProject - .NET System Brushes Painterg.FillRectangle((e.State 
& DrawItemState.Selected) == 
CodeProject - .NET System Brushes Painter  DrawItemState.Selected 
? Brushes.Yellow : Brushes.White,
CodeProject - .NET System Brushes Painter  
3, e.Bounds.Top+3, width+3, Font.Height+5);
CodeProject - .NET System Brushes Painter
CodeProject - .NET System Brushes Painterg.DrawRectangle(Pens.Black, 
3, e.Bounds.Top+3, width+3, Font.Height+5);
CodeProject - .NET System Brushes Painterg.DrawString(painter.Name, Font, Brushes.Black, 
5, e.Bounds.Top+5);
CodeProject - .NET System Brushes Painter
CodeProject - .NET System Brushes Painterbrush.Dispose();
CodeProject - .NET System Brushes Painter

We fill the area with painters brush, draw line below each item to separate them, and draw a box with painters name.

我们使用painters刷子填充区域,在每项下面画线来隔离它们,用painters的名字画了一个box

相关文章:

  • 2021-07-20
  • 2022-01-06
  • 2021-07-27
  • 2021-08-14
  • 2021-12-09
  • 2021-12-08
  • 2021-11-03
猜你喜欢
  • 2021-10-25
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2021-10-07
  • 2021-12-26
  • 2021-07-08
相关资源
相似解决方案