abstractclass DrawBase ...{ internal Color m_BackColor; internal Color m_ForeColor; internalstaticint m_HalfGrab; publicstaticint HalfGrab ...{ get...{ return DrawBase.m_HalfGrab; } set...{ DrawBase.m_HalfGrab = value; } } public Color BackColor ...{ get...{ return m_BackColor; } set...{ m_BackColor = value; } } public Color ForeColor ...{ get...{ return m_ForeColor; } set...{ m_ForeColor = value; } } publicabstract Rectangle GetBound(); publicabstractvoid Draw(Graphics g); publicabstractbool Near(int x, int y); publicabstractvoid SetBound(Rectangle bound); }
定义线:
class LineObj:DrawBase ...{ private Point m_Start; private Point m_End; public LineObj(Point start, Point end) ...{ this.m_Start = start; this.m_End = end; } publicoverride System.Drawing.Rectangle GetBound() ...{ int x =this.m_Start.X <this.m_End.X ?this.m_Start.X : this.m_End.X; int y =this.m_Start.Y <this.m_End.Y ?this.m_Start.Y : this.m_End.Y; int r =this.m_Start.X <this.m_End.X ?this.m_End.X : this.m_Start.X; int b =this.m_Start.Y <this.m_End.Y ?this.m_End.Y : this.m_Start.Y; return Rectangle.FromLTRB(x, y, r, b); } publicoverridevoid Draw(System.Drawing.Graphics g) ...{ using (Pen pen =new Pen(this.m_ForeColor)) ...{ g.DrawLine(pen, this.m_Start, this.m_End); } } publicoverridebool Near(int x, int y) ...{ //点到直线的距离是否在抓取范围之内 float A =this.m_End.Y-this.m_Start.Y; float B =this.m_End.X-this.m_Start.X; float C = B *this.m_Start.Y - A *this.m_Start.X; double D = (A * x - B * y + C) / (Math.Sqrt(A * A + B * B)); if (D >=-m_HalfGrab && D <= m_HalfGrab) ...{ RectangleF bounds =this.GetBound(); bounds.Inflate(m_HalfGrab, m_HalfGrab); return bounds.Contains(x, y); } returnfalse; } publicoverridevoid SetBound(Rectangle bound) ...{ this.m_Start =new Point(bound.X, bound.Y); this.m_End =new Point(bound.Right, bound.Bottom); } }
定义矩形:
class RectangleObj:DrawBase ...{ private Point m_Start; private Point m_End; privatebool m_Solid; public RectangleObj(Point start, Point end) ...{ this.m_Start = start; this.m_End = end; } publicbool Solid ...{ get...{ return m_Solid; } set...{ m_Solid = value; } } publicoverride System.Drawing.Rectangle GetBound() ...{ int x =this.m_Start.X <this.m_End.X ?this.m_Start.X : this.m_End.X; int y =this.m_Start.Y <this.m_End.Y ?this.m_Start.Y : this.m_End.Y; int r =this.m_Start.X <this.m_End.X ?this.m_End.X : this.m_Start.X; int b =this.m_Start.Y <this.m_End.Y ?this.m_End.Y : this.m_Start.Y; return Rectangle.FromLTRB(x, y, r, b); } publicoverridevoid Draw(Graphics g) ...{ Rectangle bound =this.GetBound(); if (this.m_Solid) ...{ using (SolidBrush brush =new SolidBrush(this.m_BackColor)) ...{ g.FillRectangle(brush, bound); } } using (Pen pen =new Pen(this.m_ForeColor)) ...{ g.DrawRectangle(pen, bound); } } publicoverridebool Near(int x, int y) ...{ Rectangle bound =this.GetBound(); Rectangle inner = bound; Rectangle outer = bound; inner.Inflate(-m_HalfGrab, -m_HalfGrab); outer.Inflate(m_HalfGrab, m_HalfGrab); Region reg =new Region(outer); reg.Exclude(inner); return reg.IsVisible(x, y); } publicoverridevoid SetBound(Rectangle bound) ...{ this.m_Start =new Point(bound.X, bound.Y); this.m_End =new Point(bound.Right, bound.Bottom); } }
当然也可以绘制Window控件:
class ButtonObj:DrawBase ...{ private Point m_Start; private Point m_End; privatestring m_Text; public ButtonObj(Point start, Point end) ...{ this.m_Start = start; this.m_End = end; } publicstring Text ...{ get...{ return m_Text; } set...{ m_Text = value; } } publicoverride System.Drawing.Rectangle GetBound() ...{ int x =this.m_Start.X <this.m_End.X ?this.m_Start.X : this.m_End.X; int y =this.m_Start.Y <this.m_End.Y ?this.m_Start.Y : this.m_End.Y; int r =this.m_Start.X <this.m_End.X ?this.m_End.X : this.m_Start.X; int b =this.m_Start.Y <this.m_End.Y ?this.m_End.Y : this.m_Start.Y; return Rectangle.FromLTRB(x, y, r, b); } publicoverridevoid Draw(Graphics g) ...{ Rectangle bound =this.GetBound(); using (Pen pen =new Pen(this.m_ForeColor)) ...{ ControlPaint.DrawButton(g, bound, ButtonState.Normal); using (SolidBrush brush =new SolidBrush(this.m_ForeColor)) ...{ using (Font font =new Font("宋体", 10)) ...{ using (StringFormat format =new StringFormat()) ...{ format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; g.DrawString(this.m_Text, font, brush, bound, format); } } } } } publicoverridebool Near(int x, int y) ...{ Rectangle bound =this.GetBound(); Rectangle outer = bound; outer.Inflate(m_HalfGrab, m_HalfGrab); return outer.Contains(x, y); } publicoverridevoid SetBound(Rectangle bound) ...{ this.m_Start =new Point(bound.X, bound.Y); this.m_End =new Point(bound.Right, bound.Bottom); } }
定义这些图像的集合对象:
class DrawList:List<DrawBase> ...{ private Control m_Owner; public DrawList(Control owner) ...{ this.m_Owner = owner; } internal DrawBase GetNear(int x, int y) ...{ foreach (DrawBase draw inthis) ...{ if (draw.Near(x, y)) ...{ return draw; } } returnnull; } internalvoid Draw(Graphics graphics) ...{ foreach (DrawBase draw inthis) ...{ draw.Draw(graphics); } } }
使用C#在Windows应用程序里绘图,可能用到移动图像、擦掉图像,调整大小等等。我这里有一个画图的小程序,简单的实现了这些。。。 定义图像的基类: abstract class DrawBase...{ internal Color m_BackColor; internal Color m_ForeColor; internal static int m_