基于 .NET Panel 类的自定义绘图应用程序






1.58/5 (19投票s)
2005年5月6日
3分钟阅读

64011

1042
基于 .NET Panel 类创建自定义绘图应用程序。
引言
本文介绍了如何创建基于 .NET Panel
类的自定义绘图应用程序。
背景
Panel
是一个 .NET 类,位于命名空间 System.Windows.Forms
中,可用于绘制/渲染多种自定义形状。它类似于 Microsoft Paint (MS Paint) 应用程序中的画布/绘图区域。可以使用 Graphics
对象绘制多种自定义形状。
为了有效地使用 .NET Panel
对象,必须实现方法 protected override void OnPaint(PaintEventArgs e)
。此方法提供了 Graphics
对象的句柄,该对象又可用于使用 FillEllipse(...)
、FillRectangle(...)
等方法绘制/渲染多种形状。
使用 Quick Draw 代码
在本文中,使用了几个类来在内存中保存多种形状。下表解释了这一点...
类名 | 描述 |
ShapeInfo |
保存形状信息,如形状、颜色和矩形对象。矩形对象保存矩形的坐标,如顶部、左侧、右侧和底部。 |
ShapeInfoCollection |
此类保存 ShapeInfo 对象的集合。 |
DrawingPanel |
这是应用程序的核心类。此类继承自 System.Windows.Forms.Panel 。 |
MainForm |
此类处理窗体事件,并融入了此应用程序的各个部分。 |
形状信息 - ShapeInfo 类
ShapeInfo
类的代码如下所示...
/// <SUMMARY>
/// Summary description for ShapeInfo.
/// </SUMMARY>
/// <SUMMARY>
/// ShapeInfo - Holds the shape information
/// like shape, color and rectangle object.
/// </SUMMARY>
[Serializable]
public class ShapeInfo
{
/// <SUMMARY>
/// Holds the rectangle object.
/// </SUMMARY>
private Rectangle _rectangle;
/// <SUMMARY>
/// Holds the color object.
/// </SUMMARY>
private Color _color;
/// <SUMMARY>
/// Holds the shape type.
/// </SUMMARY>
private int _shapeType = 0;
public ShapeInfo()
{
// Nothing to do.
}
public ShapeInfo(Rectangle rectangle, Color color, int shapeType)
{
this._rectangle = rectangle;
this._color = color;
this._shapeType = shapeType;
}
public Rectangle ShapeRectangle
{
get
{
return this._rectangle;
}
set
{
this._rectangle = value;
}
}
public Color ShapeColor
{
get
{
return this._color;
}
set
{
this._color = value;
}
}
public int Shape
{
get
{
return this._shapeType;
}
set
{
this._shapeType = value;
}
}
}
ShapeInfo
类有三个主要属性
ShapeRectangle
属性用于保存形状的位置信息。这将被用于在Panel
/Canvas
上定位特定的形状。ShapeColor
属性用于保存形状的颜色相关信息。这将被用于使用指定的颜色在Panel
/Canvas
上渲染特定的形状。Shape
属性用于保存形状的类型。它可以是 0、1 或 2,分别对应于圆形、矩形或正方形。这将被用于确定特定的形状。
在自定义 Panel 上绘图 - DrawingPanel 类
此类继承自 System.Windows.Forms.Panel
类。这用于编写自定义绘图代码。需要重写 OnPaint(PaintEventArgs e)
方法以提供 Panel
的自定义绘图信息。
/// <SUMMARY>
/// This method provides the drawing board for the graphics object.
/// </SUMMARY>
/// <PARAM name="e"></PARAM>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
SolidBrush solid = null;
// Retrieve the Ghaphics context.
Graphics g = e.Graphics;
ShapeInfo shapeInfo = null;
for(int count = 0; count < _shapeColl.Count; count++)
{
shapeInfo = (ShapeInfo) _shapeColl[count];
if (shapeInfo != null)
{
// Get the panel info object.
solid = new SolidBrush(shapeInfo.ShapeColor);
switch(shapeInfo.Shape)
{
case CIRCLE:
g.FillEllipse(solid, shapeInfo.ShapeRectangle);
break;
case RECTANGLE:
g.FillRectangle(solid, shapeInfo.ShapeRectangle);
break;
case SQUARE:
g.FillRectangle(solid,
shapeInfo.ShapeRectangle.X ,
shapeInfo.ShapeRectangle.Y,
shapeInfo.ShapeRectangle.Width,
shapeInfo.ShapeRectangle.Width);
break;
}
}
}
}
让我们逐步浏览此代码...
- 首先重写方法
OnPaint(PaintEventArgs e)
。 - 使用
PaintEventArgs
参数检索Graphics
对象。 - 使用
Graphics
对象引用来调用FillEllipse
和FillRectangle
等方法。
这将在 Panel
/Canvas
上渲染所需的形状,并且会发生重绘。
将所有部分放在一起...
确定形状后,形状信息集合将传递给 drawingPanel
,并且 drawingPanel
将失效。这允许形状在屏幕上渲染。
private void mouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (_mouseDownFlag && !_mouseUpFlag )
{
_mouseUpFlag = false;
this._shapeInfo.ShapeRectangle
= new Rectangle(_startX, _startY, (e.X - _startX), (e.Y - _startY));
drawingPanel.AddToShapeCollection(this._shapeInfo);
drawingPanel.Invalidate(this._shapeInfo.ShapeRectangle);
}
}
要点...
您已经了解了如何使用 Panel
类来在 Panel
/Canvas
上绘制/渲染多种形状。请注意,您需要调用 drawingPanel
类上的 Invalidate
方法。
请注意代码中如何进行二进制序列化以将图像保存到文件中,然后再将其恢复回来。