C#画图(WinForm)

使用C#在Windows应用程序里绘图,可能用到移动图像、擦掉图像,调整大小等等。我这里有一个画图的小程序,简单的实现了这些。。。

定义图像的基类:

abstract class DrawBase
... {
internalColorm_BackColor;
internalColorm_ForeColor;
internalstaticintm_HalfGrab;

publicstaticintHalfGrab
...{
get...{returnDrawBase.m_HalfGrab;}
set...{DrawBase.m_HalfGrab=value;}
}


publicColorBackColor
...{
get...{returnm_BackColor;}
set...{m_BackColor=value;}
}


publicColorForeColor
...{
get...{returnm_ForeColor;}
set...{m_ForeColor=value;}
}


publicabstractRectangleGetBound();
publicabstractvoidDraw(Graphicsg);
publicabstractboolNear(intx,inty);
publicabstractvoidSetBound(Rectanglebound);
}

定义线:

class LineObj:DrawBase
... {
privatePointm_Start;
privatePointm_End;
publicLineObj(Pointstart,Pointend)
...{
this.m_Start=start;
this.m_End=end;
}

publicoverrideSystem.Drawing.RectangleGetBound()
...{
intx=this.m_Start.X<this.m_End.X?this.m_Start.X:this.m_End.X;
inty=this.m_Start.Y<this.m_End.Y?this.m_Start.Y:this.m_End.Y;
intr=this.m_Start.X<this.m_End.X?this.m_End.X:this.m_Start.X;
intb=this.m_Start.Y<this.m_End.Y?this.m_End.Y:this.m_Start.Y;
returnRectangle.FromLTRB(x,y,r,b);
}


publicoverridevoidDraw(System.Drawing.Graphicsg)
...{
using(Penpen=newPen(this.m_ForeColor))
...{
g.DrawLine(pen,
this.m_Start,this.m_End);
}

}


publicoverrideboolNear(intx,inty)
...{
//点到直线的距离是否在抓取范围之内
floatA=this.m_End.Y-this.m_Start.Y;
floatB=this.m_End.X-this.m_Start.X;
floatC=B*this.m_Start.Y-A*this.m_Start.X;
doubleD=(A*x-B*y+C)/(Math.Sqrt(A*A+B*B));
if(D>=-m_HalfGrab&&D<=m_HalfGrab)
...{
RectangleFbounds
=this.GetBound();
bounds.Inflate(m_HalfGrab,m_HalfGrab);
returnbounds.Contains(x,y);
}

returnfalse;
}


publicoverridevoidSetBound(Rectanglebound)
...{
this.m_Start=newPoint(bound.X,bound.Y);
this.m_End=newPoint(bound.Right,bound.Bottom);
}

}

定义矩形:

class RectangleObj:DrawBase
... {
privatePointm_Start;
privatePointm_End;
privateboolm_Solid;
publicRectangleObj(Pointstart,Pointend)
...{
this.m_Start=start;
this.m_End=end;
}

publicboolSolid
...{
get...{returnm_Solid;}
set...{m_Solid=value;}
}

publicoverrideSystem.Drawing.RectangleGetBound()
...{
intx=this.m_Start.X<this.m_End.X?this.m_Start.X:this.m_End.X;
inty=this.m_Start.Y<this.m_End.Y?this.m_Start.Y:this.m_End.Y;
intr=this.m_Start.X<this.m_End.X?this.m_End.X:this.m_Start.X;
intb=this.m_Start.Y<this.m_End.Y?this.m_End.Y:this.m_Start.Y;
returnRectangle.FromLTRB(x,y,r,b);
}


publicoverridevoidDraw(Graphicsg)
...{
Rectanglebound
=this.GetBound();
if(this.m_Solid)
...{
using(SolidBrushbrush=newSolidBrush(this.m_BackColor))
...{
g.FillRectangle(brush,bound);
}

}

using(Penpen=newPen(this.m_ForeColor))
...{
g.DrawRectangle(pen,bound);
}

}


publicoverrideboolNear(intx,inty)
...{
Rectanglebound
=this.GetBound();
Rectangleinner
=bound;
Rectangleouter
=bound;
inner.Inflate(
-m_HalfGrab,-m_HalfGrab);
outer.Inflate(m_HalfGrab,m_HalfGrab);
Regionreg
=newRegion(outer);
reg.Exclude(inner);
returnreg.IsVisible(x,y);
}


publicoverridevoidSetBound(Rectanglebound)
...{
this.m_Start=newPoint(bound.X,bound.Y);
this.m_End=newPoint(bound.Right,bound.Bottom);
}

}

当然也可以绘制Window控件:

class ButtonObj:DrawBase
... {
privatePointm_Start;
privatePointm_End;
privatestringm_Text;
publicButtonObj(Pointstart,Pointend)
...{
this.m_Start=start;
this.m_End=end;
}

publicstringText
...{
get...{returnm_Text;}
set...{m_Text=value;}
}

publicoverrideSystem.Drawing.RectangleGetBound()
...{
intx=this.m_Start.X<this.m_End.X?this.m_Start.X:this.m_End.X;
inty=this.m_Start.Y<this.m_End.Y?this.m_Start.Y:this.m_End.Y;
intr=this.m_Start.X<this.m_End.X?this.m_End.X:this.m_Start.X;
intb=this.m_Start.Y<this.m_End.Y?this.m_End.Y:this.m_Start.Y;
returnRectangle.FromLTRB(x,y,r,b);
}


publicoverridevoidDraw(Graphicsg)
...{
Rectanglebound
=this.GetBound();
using(Penpen=newPen(this.m_ForeColor))
...{
ControlPaint.DrawButton(g,bound,ButtonState.Normal);
using(SolidBrushbrush=newSolidBrush(this.m_ForeColor))
...{
using(Fontfont=newFont("宋体",10))
...{
using(StringFormatformat=newStringFormat())
...{
format.Alignment
=StringAlignment.Center;
format.LineAlignment
=StringAlignment.Center;
g.DrawString(
this.m_Text,font,brush,bound,format);
}

}

}

}

}


publicoverrideboolNear(intx,inty)
...{
Rectanglebound
=this.GetBound();
Rectangleouter
=bound;
outer.Inflate(m_HalfGrab,m_HalfGrab);
returnouter.Contains(x,y);
}


publicoverridevoidSetBound(Rectanglebound)
...{
this.m_Start=newPoint(bound.X,bound.Y);
this.m_End=newPoint(bound.Right,bound.Bottom);
}

}

定义这些图像的集合对象:

class DrawList:List < DrawBase >
... {
privateControlm_Owner;
publicDrawList(Controlowner)
...{
this.m_Owner=owner;
}

internalDrawBaseGetNear(intx,inty)
...{
foreach(DrawBasedrawinthis)
...{
if(draw.Near(x,y))
...{
returndraw;
}

}

returnnull;
}


internalvoidDraw(Graphicsgraphics)
...{
foreach(DrawBasedrawinthis)
...{
draw.Draw(graphics);
}

}

}

构建一个Window窗体用来展示图像操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrawLines.DrawObjs;

namespace DrawLines
... {
publicpartialclassForm1:Form
...{
privateDrawListm_drawList;
privateDrawBasem_curDraw;
privateRectanglem_curBound;
privateRectanglem_lastBound;
privatePointm_StartPoint;
publicForm1()
...{
InitializeComponent();
this.SetStyle(ControlStyles.OptimizedDoubleBuffer|ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint,true);

this.InitObjs();
}

/**////<summary>
///构造对象
///</summary>

privatevoidInitObjs()
...{
this.m_drawList=newDrawList(this);

DrawBase.HalfGrab
=5;
LineObjline0
=newLineObj(newPoint(10,10),newPoint(100,100));
line0.ForeColor
=Color.Red;

LineObjline1
=newLineObj(newPoint(16,67),newPoint(100,180));
line1.ForeColor
=Color.Blue;

RectangleObjrect
=newRectangleObj(newPoint(120,70),newPoint(220,200));
rect.ForeColor
=Color.Green;

ButtonObjbutton
=newButtonObj(newPoint(20,20),newPoint(90,45));
button.ForeColor
=Color.Black;
button.Text
="button1";
m_drawList.Add(line0);
m_drawList.Add(line1);
m_drawList.Add(rect);
m_drawList.Add(button);
}

/**////<summary>
///绘制对象
///</summary>
///<paramname="e"></param>

protectedoverridevoidOnPaint(PaintEventArgse)
...{
this.m_drawList.Draw(e.Graphics);
base.OnPaint(e);
}

/**////<summary>
///重写以移动对象或设置鼠标
///</summary>
///<paramname="e"></param>

protectedoverridevoidOnMouseMove(MouseEventArgse)
...{
base.OnMouseMove(e);
if(this.Capture)
...{
if(this.m_curDraw!=null)
...{
this.moveObj(e);
}

return;
}

if(this.m_drawList.GetNear(e.X,e.Y)!=null)
...{
this.Cursor=Cursors.SizeAll;
}

else
...{
this.Cursor=Cursors.Default;
}

}

/**////<summary>
///重写以获取鼠标下的对象
///</summary>
///<paramname="e"></param>

protectedoverridevoidOnMouseDown(MouseEventArgse)
...{
base.OnMouseDown(e);
this.getObj(e);
}

/**////<summary>
///重写以按Delete键删除当前对象
///</summary>
///<paramname="keyData"></param>
///<returns></returns>

protectedoverrideboolProcessDialogKey(KeyskeyData)
...{
if(keyData==Keys.Delete)
...{
if(this.m_curDraw!=null)
...{
this.deleteObj();
}

}

returnbase.ProcessDialogKey(keyData);
}

privatevoidbtnDelete_Click(objectsender,EventArgse)
...{
if(this.m_curDraw!=null)
...{
this.deleteObj();
}

else
...{
MessageBox.Show(
this,"没有选中的对象,请使用鼠标点选择一个!","提示");
}

}

privatevoidmoveObj(MouseEventArgse)
...{
Rectanglebound
=this.m_curBound;

bound.Offset(e.X
-this.m_StartPoint.X,e.Y-this.m_StartPoint.Y);
this.m_curDraw.SetBound(bound);

Rectangle_last
=this.m_lastBound;
Rectangle_bound
=bound;
_last.Inflate(
2,2);
_bound.Inflate(
2,2);

using(RegioninvReg=newRegion(_last))
...{
invReg.Union(_bound);
this.Invalidate(invReg);
}

this.m_lastBound=bound;
}

privatevoidgetObj(MouseEventArgse)
...{
this.m_curDraw=this.m_drawList.GetNear(e.X,e.Y);
if(this.m_curDraw!=null)
...{
this.m_curBound=this.m_curDraw.GetBound();
this.m_StartPoint=e.Location;
}

}

privatevoiddeleteObj()
...{
Rectanglebound
=this.m_curDraw.GetBound();
this.m_drawList.Remove(this.m_curDraw);
this.m_curDraw=null;
bound.Inflate(
2,2);
this.Invalidate(bound);
}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值