封装的一些常用图形的绘制类:
/// <summary>
/// 选择图片类
/// </summary>
public class DrawChoose
{
#region 选择图片
/// <summary>
/// 选择图片
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public static void drawChoose(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(Color.DodgerBlue))
{
pen.DashStyle = DashStyle.Custom;
pen.DashPattern = new float[] { 1f, 1f };
graphics.DrawRectangle(pen, startPoint.X, startPoint.Y,
endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
graphics.FillRectangle(Brushes.DodgerBlue,
new Rectangle(startPoint.X - 3, startPoint.Y - 3, 3, 3));
graphics.FillRectangle(Brushes.DodgerBlue,
new Rectangle(endPoint.X, endPoint.Y, 3, 3));
graphics.FillRectangle(Brushes.DodgerBlue,
new Rectangle(endPoint.X, startPoint.Y - 3, 3,3));
graphics.FillRectangle(Brushes.DodgerBlue,
new Rectangle(startPoint.X - 3, endPoint.Y, 3, 3));
graphics.FillRectangle(Brushes.DodgerBlue,
new Rectangle((startPoint.X + endPoint.X) / 2 - 1, startPoint.Y -3, 3, 3));
graphics.FillRectangle(Brushes.DodgerBlue,
new Rectangle((startPoint.X + endPoint.X) / 2 - 1, endPoint.Y, 3, 3));
graphics.FillRectangle(Brushes.DodgerBlue,
new Rectangle(startPoint.X -3, (startPoint.Y + endPoint.Y) / 2, 3, 3));
graphics.FillRectangle(Brushes.DodgerBlue,
new Rectangle(endPoint.X, (startPoint.Y + endPoint.Y) / 2, 3, 3));
}
}
#endregion
}
/// <summary>
/// 实心椭圆类/// </summary>
[Serializable]public class Circle:ShapeBase
{
#region 绘制实心椭圆
/// <summary>
/// 绘制实心椭圆
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
graphics.FillEllipse(new SolidBrush(color), startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 图片类
/// </summary>
[Serializable]public class DrawImage:Rectangles
{
private Image myImage;
/// <summary>
/// 存储图片数据
/// </summary>
public Image MyImage
{
get { return myImage; }
set { myImage = value; }
}
}
#region 绘制图片
/// <summary>
/// 绘制图片
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
graphics.DrawImage(myImage, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 实心矩形类
/// </summary>
[Serializable]public class Orthogon : ShapeBase
{
#region 绘制实心矩形
/// <summary>
/// 绘制实心矩形
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color))
{
graphics.FillRectangle(new SolidBrush(color), startPoint.X, startPoint.Y,
endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 空心矩形类
/// </summary>
[Serializable]public class Rectangles : ShapeBase
{
#region 绘制空心矩形
/// <summary>
/// 绘制空心矩形
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
graphics.DrawRectangle(pen, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 空心直角三角形类
/// </summary>
[Serializable]
public class RightTriangle : ShapeBase
{
#region 绘制空心直角三角形
/// <summary>
/// 绘制空心直角三角形
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
Point leftPoint = new Point(startPoint.X,endPoint.Y);
Point[] pointArry = { startPoint, leftPoint, endPoint };
graphics.DrawPolygon(pen, pointArry);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 空心椭圆类
/// </summary>
[Serializable]
public class Roundness : ShapeBase
{
#region 绘制空心椭圆
/// <summary>
/// 绘制空心椭圆
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 实心直角三角形类
/// </summary>
[Serializable]
public class SolidRightTriangle : ShapeBase
{
#region 绘制实心直角三角形
/// <summary>
/// 绘制实心直角三角形
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
Point leftPoint = new Point(startPoint.X, endPoint.Y);
Point[] pointArry = { startPoint, leftPoint, endPoint };
graphics.FillPolygon(new SolidBrush(color), pointArry);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 直线类
/// </summary>
[Serializable]
public class StraightLine : ShapeBase
{
#region 绘制直线
/// <summary>
/// 绘制直线
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
graphics.DrawLine(pen, startPoint, endPoint); //两个坐标连成直线
}
if (this.StraightLineChecked)
{
using (Pen pen = new Pen(Color.DodgerBlue))
{
graphics.FillRectangle(Brushes.DodgerBlue, new Rectangle(startPoint.X - 2, startPoint.Y - 1, size + 2, size + 1));
graphics.FillRectangle(Brushes.DodgerBlue, new Rectangle(endPoint.X - 2, endPoint.Y - 2, size + 1, size + 1));
}
}
}
#endregion
}
/// <summary>
/// 空心三角形类
/// </summary>
[Serializable]
public class Triangle : ShapeBase
{
#region 绘制空心三角形
/// <summary>
/// 绘制空心三角形
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
Point leftPoint = new Point(startPoint.X, endPoint.Y);
Point topPoint = new Point(startPoint.X + (endPoint.X - startPoint.X) / 2, startPoint.Y);
Point[] pointArry = { leftPoint, topPoint, endPoint };
graphics.DrawPolygon(pen, pointArry);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 实心三角形类
/// </summary>
[Serializable]
public class Trilateral : ShapeBase
{
#region 绘制实心三角形
/// <summary>
/// 绘制实心三角形
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
Point leftPoint = new Point(startPoint.X, endPoint.Y);
Point topPoint = new Point(startPoint.X + (endPoint.X - startPoint.X) / 2, startPoint.Y);
Point[] pointArry = { leftPoint, topPoint, endPoint };
graphics.FillPolygon(new SolidBrush(color), pointArry);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
}
}
#endregion
}
/// <summary>
/// 文字类
/// </summary>
[Serializable]
public class Word : ShapeBase
{
#region 字段和属性
private string text;
/// <summary>
/// 文本内容
/// </summary>
public string Text
{
get { return text; }
set { text = value; }
}
private int wordSize = 8;
/// <summary>
/// 字体大小
/// 默认大小为8
/// </summary>
public int WordSize
{
get { return wordSize; }
set { wordSize = value; }
}
#endregion
#region 绘制文字
/// <summary>
/// 绘制文字
/// </summary>
/// <param name="graphics">画板</param>
/// <param name="startPoint">起始点</param>
/// <param name="endPoint">结束点</param>
/// <param name="color">颜色</param>
/// <param name="size">粗细大小</param>
public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
{
using (Pen pen = new Pen(color, size))
{
graphics.DrawString(text, new Font("SimSun", wordSize), new SolidBrush(color), startPoint.X, startPoint.Y);
}
if (this.StraightLineChecked)
{
DrawChoose.drawChoose(graphics,startPoint,endPoint,color,size);
}
}
#endregion
}