利用鼠标绘图

实现效果:

  

知识运用:

  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;
        }

  

原文地址:https://www.cnblogs.com/feiyucha/p/10289259.html