当前位置: 首页 > 图文教程 > .Net技术 > C# > c#:C#技术利用鼠标绘图
本文章主要是利用窗体MouseDown、MouseUp事件及Pen、Graphics等类实现的。运行程序,当鼠标按下时,即绘图开始,记录绘图的起点坐标;当鼠标释放时,即绘图结束,记录绘图的终点坐标;当鼠标移动时,即绘制一条从起点到终点的直线。
下面给出的是主要代码:
Public Form1()
{
InitializeComponent();
Pen=new Pen(Color.FromName(“black”));//始末画笔
Graphics=CreateGraphics();//初始画板
}
Public bool G_OnMouseDown=false;
Public point lastPoint =Point.Empty;
Public pen pen;
Public Grapics graphics;
Private void Form1_MouseMove(object sender,MouseEventArgs e)
{
If(lastPoint.Equals(point.Empty))
{lastPont = new Point(e.X,e.Y);}
If(G_OnMouseDown)
{
Point cruuPoint = new Point(e.X,e.Y);
Graphics.DrawLine(pen,cruuPoint,lastPoint);
}
lastPont =new Point(e.X,e.Y);
}
//当鼠标离开时把布尔变量设成false
Private void Form1_MouseUp(object sender,MouseEventArgs e)
{
G_OnMouseDown=false;
}
Private void Form1_MouseDown(object sender,MouseEventArgs e)
{
G_OnMouseDown=true;
}
评论 (0) All