你代码有用到  Paint参数  clipBounds 吗?

解决方案 »

  1.   

    只是重新绘制窗口而已
    protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                if (arr.Count > 2)
                    for (int i = 1; i < arr.Count; i++)
                        g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i]));
            }
      

  2.   

    能画出来还是会抛出异常?我的机器就是有异常死憋着不放,上次有个数组越界的异常也不抛出,后来直接运行程序的时候才发现,就是不知道这到底是什么情况
    没异常 一切运行正常 根据你问题改变窗口啥的,你可以发相对全一点代码上来看看
    去掉了窗口上的一个按钮,这是完整的源代码:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    using System.Threading;namespace 鼠标划线
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                p = new Point();
                arr = new ArrayList();
            }
            bool bmove=false;
            Point p;
            Graphics g;
            ArrayList arr;
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                g = this.CreateGraphics();
                bmove = true;
                p = new Point(e.X,e.Y);
                arr.Add(p);
            }
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (bmove)
                {  
                    g.DrawLine(Pens.Red,p,new Point(e.X,e.Y));
                    p = new Point(e.X,e.Y);
                    arr.Add(p);
                }           
            }        private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                bmove = false;
                g.Dispose();
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                if (arr.Count > 2)
                    for (int i = 1; i < arr.Count; i++)
                        g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i]));
            }        private void Form1_Paint(object sender, PaintEventArgs e)
            {
            }
        }
    }
      

  3.   

    当你MouseUp的时候 g.Dispose();释放了  所以执行下面语句会出错for (int i = 1; i < arr.Count; i++)
        g.DrawLine(Pens.Red, (Point)arr[i - 1], ((Point)arr[i]));
     
      

  4.   

    你代码可以这样简化public partial class Form1 : Form
        {
            IList<Point> arr;
            public Form1()
            {
                InitializeComponent();            DoubleBuffered = true;
                arr = new List<Point>();
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    arr.Add(new Point(e.X, e.Y));
                    this.Invalidate();
                }
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                if (arr.Count == 1)
                {
                    e.Graphics.FillRectangle(Brushes.Red, arr[0].X, arr[0].Y, 1, 1);
                }
                else
                {
                    for (int i = 1; i < arr.Count; i++)
                        e.Graphics.DrawLine(Pens.Red, arr[i - 1], arr[i]);
                }
            }
        }
      

  5.   

    这个代码好像只能把所有的线画成一条,每次鼠标重新点击都会把原来的线和现在的线连成一条,我把它改了一下,你看行不?
        public partial class Form1 : Form
        {
            List<Point> arr;
            IList<IList> arr_a;
            public Form1()
            {
                InitializeComponent();            DoubleBuffered = true;
                arr = new List<Point>();
                arr_a = new List<IList>();
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    arr.Add(new Point(e.X, e.Y));                
                    this.Invalidate();
                }
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                if (arr_a.Count == 0)
                {
                    for (int i = 1; i < arr.Count; i++)
                        e.Graphics.DrawLine(Pens.Red, arr[i - 1], arr[i]);
                }
                else
                {
                    for (int i = 0; i < arr_a.Count; i++)
                        for (int j = 1; j < arr_a[i].Count; j++)
                            e.Graphics.DrawLine(Pens.Red, (Point)(arr_a[i])[j - 1], (Point)(arr_a[i])[j]);
                }
            }        private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                arr = new List<Point>();
                List<Point> newarr = arr;
                arr_a.Add(newarr);
                
            }
        }
      

  6.   


    Form1_MouseDown:
      arr = new List<Point>();
      arr_a.Add(arr);