利用鼠标绘图
Posted feiyucha
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用鼠标绘图相关的知识,希望对你有一定的参考价值。
实现效果:
知识运用:
Graphics类的DrawLine方法和MouseEventArgs类的x,y属性
实现代码:
private void Form1_MouseMove(object sender, MouseEventArgs e) { if (lastPoint.Equals(Point.Empty)) //判断绘图开始点是否为空 { lastPoint = new Point(e.X,e.Y); //记录鼠标当前位置 } if (onMouseDown) //开始绘图 { graphics = this.CreateGraphics(); Point currPoint = new Point(e.X, e.Y); //获取鼠标当前位置 graphics.DrawLine(new Pen (Color.Black),lastPoint,currPoint); //绘图 } lastPoint = new Point(e.X,e.Y); //更新绘图点 } private void Form1_MouseDown(object sender, MouseEventArgs e) { onMouseDown = true; //开始绘图标识设为true; } private void Form1_MouseUp(object sender, MouseEventArgs e) { onMouseDown = false; //开始绘图标识设为false; }
以上是关于利用鼠标绘图的主要内容,如果未能解决你的问题,请参考以下文章