在c#里怎么实现移动鼠标自由画图的功能,就像windows画图程序里用铅笔画这样。我用过在mousemove事件里根据鼠标坐标来画,但是这种方式如果鼠标移动快了,画出来的线就不连续了,有没有什么好的方法。

解决方案 »

  1.   


            bool isStartToDraw = false;
            Point startPoint = new Point();        private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                isStartToDraw = true;
                startPoint = e.Location;
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (isStartToDraw)
                {
                    Graphics graphics = this.CreateGraphics();
                    graphics.DrawLine(Pens.Black, startPoint, e.Location);
                    startPoint = e.Location;
                }
            }        private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                isStartToDraw = false;
            }
      

  2.   

    有个东西叫做GDI,以前用java也画过线
    protected   override   void   OnPaint(PaintEventArgs   pe)   
      {   
            Graphics   g   =   pe.Graphics;   
            Pen   pen   =   new   Pen(Color.Blue);     
            g.DrawRectangle(pen,3,3,button1.Width-8,button1.Height-8);     
            base.Paint(pe);   
      } 
      

  3.   

    绘图画面对象是手动创建的,所以需要手动Dispose            if (isStartToDraw)
                {
                    Graphics graphics = this.CreateGraphics();
                    graphics.DrawLine(Pens.Black, startPoint, e.Location);
                    startPoint = e.Location;
                    graphics.Dispose();
                }
      

  4.   

    把你MouseMove的点都记录下来,然后调用
    graphics.DrawCurve方法就可以了