前言
该画笔程序是自己制作的一个小程序,功能基本完善,但是与专业软件还是存在着一定差距的。该软件具有画直线、矩形、正方形、椭圆、圆、徒手画(随意画)、屏幕写字、屏幕画笔(截屏后在截图上作图)、停止绘图、撤销、重做、设置线宽、设置颜色、放大、缩小、文件另存为(保存)、作图保存为图片、保存用户设置等功能。
界面效果图
图1
部分代码
using System
;
using System
.Collections
.Generic
;
using System
.Linq
;
using System
.Text
;
using System
.Threading
.Tasks
;
using System
.Drawing
;
using System
.Drawing
.Drawing2D
;
using System
.IO
;
using System
.Windows
.Forms
;
namespace MyPencil
{
public
enum DrawType
{
Stop
= 0, Line
= 1, Rectangle
= 2, Circle
= 3, Ellipse
= 4, Square
= 5, Sketch
= 6, Writing
= 7
}
public abstract class Shape
{
private
int _penWidth
= 10;
private Color _penColor
= Color
.Red
;
public
int _PenWidth
{ get
=> _penWidth
; set
=> _penWidth
= value
; }
public Color _PenColor
{ get
=> _penColor
; set
=> _penColor
= value
; }
public abstract
void Draw(Graphics g
, DashStyle ds
, double zoomRatio
);
public abstract
void Write(BinaryWriter bw
);
public abstract
void Read(BinaryReader br
);
}
public class Line
: Shape
{
private Point _p1
;
private Point _p2
;
public Point _P1
{ get
=> _p1
; set
=> _p1
= value
; }
public Point _P2
{ get
=> _p2
; set
=> _p2
= value
; }
public
Line()
{
}
public
Line(Point p1
, Point p2
)
{
_p1
= p1
;
_p2
= p2
;
}
public override
void Draw(Graphics g
, DashStyle ds
, double zoomRatio
)
{
Pen pen
= new
Pen(_PenColor
, (int)(_PenWidth
* zoomRatio
));
pen
.DashStyle
= ds
;
Point p1
= new
Point((int)(_p1
.X
* zoomRatio
), (int)(_p1
.Y
* zoomRatio
));
Point p2
= new
Point((int)(_p2
.X
* zoomRatio
), (int)(_p2
.Y
* zoomRatio
));
g
.DrawLine(pen
, p1
, p2
);
}
public override
void Write(BinaryWriter bw
)
{
bw
.Write(_PenWidth
);
bw
.Write(_PenColor
.ToArgb());
bw
.Write(_p1
.X
); bw
.Write(_p1
.Y
);
bw
.Write(_p2
.X
); bw
.Write(_p2
.Y
);
}
public override
void Read(BinaryReader br
)
{
_PenWidth
= br
.ReadInt32();
_PenColor
= Color
.FromArgb(br
.ReadInt32());
_p1
.X
= br
.ReadInt32(); _p1
.Y
= br
.ReadInt32();
_p2
.X
= br
.ReadInt32(); _p2
.Y
= br
.ReadInt32();
}
}
public class Rectangle
:Shape
{
private Point _p1
;
private Point _p2
;
public Point _P1
{ get
=> _p1
; set
=> _p1
= value
; }
public Point _P2
{ get
=> _p2
; set
=> _p2
= value
; }
public
Rectangle()
{
}
public
Rectangle(Point p1
, Point p2
)
{
_p1
= p1
;
_p2
= p2
;
}
public override
void Draw(Graphics g
, DashStyle ds
, double zoomRatio
)
{
Pen pen
= new
Pen(_PenColor
, (int)(_PenWidth
* zoomRatio
));
pen
.DashStyle
= ds
;
Point p1
= new
Point((int)(_p1
.X
* zoomRatio
), (int)(_p1
.Y
* zoomRatio
));
Point p2
= new
Point((int)(_p2
.X
* zoomRatio
), (int)(_p2
.Y
* zoomRatio
));
Point leftTop
= new
Point();
leftTop
.X
= (p1
.X
<= p2
.X
) ? p1
.X
: p2
.X
;
leftTop
.Y
= (p1
.Y
<= p2
.Y
) ? p1
.Y
: p2
.Y
;
g
.DrawRectangle(pen
, leftTop
.X
, leftTop
.Y
, Math
.Abs(p2
.X
- p1
.X
), Math
.Abs(p2
.Y
- p1
.Y
));
}
public override
void Write(BinaryWriter bw
)
{
bw
.Write(_PenWidth
);
bw
.Write(_PenColor
.ToArgb());
bw
.Write(_p1
.X
); bw
.Write(_p1
.Y
);
bw
.Write(_p2
.X
); bw
.Write(_p2
.Y
);
}
public override
void Read(BinaryReader br
)
{
_PenWidth
= br
.ReadInt32();
_PenColor
= Color
.FromArgb(br
.ReadInt32());
_p1
.X
= br
.ReadInt32(); _p1
.Y
= br
.ReadInt32();
_p2
.X
= br
.ReadInt32(); _p2
.Y
= br
.ReadInt32();
}
}
public class Square
: Shape
{
private Point _p1
;
private Point _p2
;
public Point _P1
{ get
=> _p1
; set
=> _p1
= value
; }
public Point _P2
{ get
=> _p2
; set
=> _p2
= value
; }
public
Square()
{
}
public
Square(Point p1
, Point p2
)
{
_p1
= p1
;
_p2
= p2
;
}
public override
void Draw(Graphics g
, DashStyle ds
, double zoomRatio
)
{
Pen pen
= new
Pen(_PenColor
, (int)(_PenWidth
* zoomRatio
));
pen
.DashStyle
= ds
;
Point p1
= new
Point((int)(_p1
.X
* zoomRatio
), (int)(_p1
.Y
* zoomRatio
));
Point p2
= new
Point((int)(_p2
.X
* zoomRatio
), (int)(_p2
.Y
* zoomRatio
));
float length
= (float)(Math
.Sqrt(Math
.Pow(p2
.X
- p1
.X
, 2) + Math
.Pow(p2
.Y
- p1
.Y
, 2)) / Math
.Sqrt(2));
Point stablePoint
= new
Point();
if(p1
.X
<= p2
.X
&& p1
.Y
<= p2
.Y
)
{
stablePoint
.X
= p1
.X
;
stablePoint
.Y
= p1
.Y
;
}
else if(p1
.X
<= p2
.X
&& p1
.Y
>= p2
.Y
)
{
stablePoint
.X
= p1
.X
;
stablePoint
.Y
= (int)(p1
.Y
- length
);
}
else if(p1
.X
>= p2
.X
&& p1
.Y
<= p2
.Y
)
{
stablePoint
.X
= (int)(p1
.X
- length
);
stablePoint
.Y
= p1
.Y
;
}
else
{
stablePoint
.X
= (int)(p1
.X
- length
);
stablePoint
.Y
= (int)(p1
.Y
- length
);
}
g
.DrawRectangle(pen
, stablePoint
.X
, stablePoint
.Y
, length
, length
) ;
}
public override
void Write(BinaryWriter bw
)
{
bw
.Write(_PenWidth
);
bw
.Write(_PenColor
.ToArgb());
bw
.Write(_p1
.X
); bw
.Write(_p1
.Y
);
bw
.Write(_p2
.X
); bw
.Write(_p2
.Y
);
}
public override
void Read(BinaryReader br
)
{
_PenWidth
= br
.ReadInt32();
_PenColor
= Color
.FromArgb(br
.ReadInt32());
_p1
.X
= br
.ReadInt32(); _p1
.Y
= br
.ReadInt32();
_p2
.X
= br
.ReadInt32(); _p2
.Y
= br
.ReadInt32();
}
}
public class Circle
:Shape
{
private Point _pCenter
;
private
float _r
;
public
Circle()
{
}
public
Circle(Point pCenter
, float r
)
{
_pCenter
= pCenter
;
_r
= r
;
}
public Point _PCenter
{ get
=> _pCenter
; set
=> _pCenter
= value
; }
public
float _R
{ get
=> _r
; set
=> _r
= value
; }
public override
void Draw(Graphics g
, DashStyle ds
, double zoomRatio
)
{
Pen pen
= new
Pen(_PenColor
, (int)(_PenWidth
* zoomRatio
));
pen
.DashStyle
= ds
;
Point pCenter
= new
Point((int)(_pCenter
.X
* zoomRatio
), (int)(_pCenter
.Y
* zoomRatio
));
float r
= (float)(_r
* zoomRatio
);
g
.DrawEllipse(pen
, pCenter
.X
- r
, pCenter
.Y
- r
, 2 * r
, 2 * r
);
}
public override
void Write(BinaryWriter bw
)
{
bw
.Write(_PenWidth
);
bw
.Write(_PenColor
.ToArgb());
bw
.Write(_pCenter
.X
);
bw
.Write(_pCenter
.Y
);
bw
.Write(_r
);
}
public override
void Read(BinaryReader br
)
{
_PenWidth
= br
.ReadInt32();
_PenColor
= Color
.FromArgb(br
.ReadInt32());
_pCenter
.X
= br
.ReadInt32();
_pCenter
.Y
= br
.ReadInt32();
_r
= br
.ReadSingle();
}
}
public class Ellipse
:Shape
{
private Point _pCenter
;
private
float _halfR
;
public
Ellipse()
{
}
public
Ellipse(Point pCenter
, float halfR
)
{
_pCenter
= pCenter
;
_halfR
= halfR
;
}
public Point _PCenter
{ get
=> _pCenter
; set
=> _pCenter
= value
; }
public
float _HalfR
{ get
=> _halfR
; set
=> _halfR
= value
; }
public override
void Draw(Graphics g
, DashStyle ds
, double zoomRatio
)
{
Pen pen
= new
Pen(_PenColor
, (int)(_PenWidth
* zoomRatio
));
pen
.DashStyle
= ds
;
Point pCenter
= new
Point((int)(_pCenter
.X
* zoomRatio
), (int)(_pCenter
.Y
* zoomRatio
));
float halfR
= (float)(_halfR
* zoomRatio
);
g
.DrawEllipse(pen
, pCenter
.X
- halfR
, pCenter
.Y
- halfR
, 2 * halfR
, 3 * halfR
);
}
public override
void Write(BinaryWriter bw
)
{
bw
.Write(_PenWidth
);
bw
.Write(_PenColor
.ToArgb());
bw
.Write(_pCenter
.X
);
bw
.Write(_pCenter
.Y
);
bw
.Write(_halfR
);
}
public override
void Read(BinaryReader br
)
{
_PenWidth
= br
.ReadInt32();
_PenColor
= Color
.FromArgb(br
.ReadInt32());
_pCenter
.X
= br
.ReadInt32();
_pCenter
.Y
= br
.ReadInt32();
_halfR
= br
.ReadSingle();
}
}
public class Sketch
: Shape
{
private List
<Point
> _pointList
= new List
<Point
>();
public List
<Point
> _PointList
{
get
{ return _pointList
; }
set
{ _pointList
= value
; }
}
public
Sketch()
{
}
public override
void Draw(Graphics g
, DashStyle ds
, double zoomRatio
)
{
Pen pen
= new
Pen(_PenColor
, (int)(_PenWidth
* zoomRatio
));
pen
.DashStyle
= ds
;
pen
.StartCap
= LineCap
.Round
;
pen
.EndCap
= LineCap
.Round
;
pen
.LineJoin
= LineJoin
.Round
;
g
.SmoothingMode
= SmoothingMode
.AntiAlias
;
for (int i
= 1; i
<= _pointList
.Count
- 1; i
++)
{
List
<Point
> tempList
= new List
<Point
>();
foreach
(Point point in _pointList
)
tempList
.Add(new
Point((int)(point
.X
* zoomRatio
), ((int)(point
.Y
* zoomRatio
))));
g
.DrawCurve(pen
, tempList
.ToArray());
}
}
public override
void Write(BinaryWriter binaryWriter
)
{
binaryWriter
.Write(_PenWidth
);
binaryWriter
.Write(_PenColor
.ToArgb());
binaryWriter
.Write(_pointList
.Count());
foreach
(Point tempPoint in _pointList
)
{
binaryWriter
.Write(tempPoint
.X
);
binaryWriter
.Write(tempPoint
.Y
);
}
}
public override
void Read(BinaryReader binaryReader
)
{
_pointList
.Clear();
_PenWidth
= binaryReader
.ReadInt32();
_PenColor
= Color
.FromArgb(binaryReader
.ReadInt32());
int pointCount
= binaryReader
.ReadInt32();
for (int i
= 0; i
<= pointCount
- 1; i
++)
{
int x
= binaryReader
.ReadInt32();
int y
= binaryReader
.ReadInt32();
Point point
= new
Point(x
, y
);
_pointList
.Add(point
);
}
}
}
public class WritingFont
: Shape
{
private Font _fontStyleSize
;
private string _fontText
;
private Point _fontLocation
;
public Font _FontStyleSize
{ get
=> _fontStyleSize
; set
=> _fontStyleSize
= value
; }
public string _FontText
{ get
=> _fontText
; set
=> _fontText
= value
; }
public Point _FontLocation
{ get
=> _fontLocation
; set
=> _fontLocation
= value
; }
public
WritingFont()
{
}
public override
void Draw(Graphics g
, DashStyle ds
, double zoomRatio
)
{
g
.DrawString(_fontText
, _fontStyleSize
, new
SolidBrush(_PenColor
), (int)(_fontLocation
.X
* zoomRatio
), (int)(_fontLocation
.Y
* zoomRatio
));
}
public override
void Write(BinaryWriter binaryWriter
)
{
binaryWriter
.Write((int)(_fontStyleSize
.Size
));
binaryWriter
.Write(_fontStyleSize
.FontFamily
.Name
.ToString());
binaryWriter
.Write(_PenColor
.ToArgb());
binaryWriter
.Write(_fontLocation
.X
);
binaryWriter
.Write(_fontLocation
.Y
);
binaryWriter
.Write(_fontText
.ToString());
}
public override
void Read(BinaryReader binaryReader
)
{
int size
= binaryReader
.ReadInt32();
string name
= binaryReader
.ReadString();
_fontStyleSize
= new
Font(name
, size
, FontStyle
.Regular
);
_PenColor
= Color
.FromArgb(binaryReader
.ReadInt32());
_fontLocation
.X
= binaryReader
.ReadInt32();
_fontLocation
.Y
= binaryReader
.ReadInt32();
_fontText
= binaryReader
.ReadString();
}
}
}
完整文件
画笔程序完整文件