C#鼠标移动画线的两种方式

1.直线:
C#鼠标移动画线的两种方式_第1张图片

		public Point start, end;
        public bool Myflag = false;

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
     
            this.Myflag = false;         
        }   
        
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
     
            if(Myflag == false)
            {
     
                start.X = e.X;
                start.Y = e.Y;
            }
            Myflag = true;
            end.X = e.X;
            end.Y = e.Y;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
                 
            Graphics g = this.CreateGraphics();
            Pen Mypen1 = new Pen(Color.Black);
            Pen Mypen2 = new Pen(this.BackColor);

            if(Myflag == false)
            {
     
                return;
            }
            //用背景色画一遍,即擦除原来的线
            g.DrawLine(Mypen2, start.X, start.Y, end.X, end.Y); 
            this.end.X = e.X;
            this.end.Y = e.Y;
                       
            g.DrawLine(Mypen1, start.X, start.Y, end.X, end.Y);
        }
    }

2.随着鼠标移动画线https://blog.csdn.net/hui_shixu/article/details/7738410
我第一个画直线的方法也有受到这篇的启发,注释也很详细易懂(这个直线相对不好控制,但是可以画曲线)

你可能感兴趣的:(c#,gui,wpf,winform)