当前位置: 代码迷 >> C# >> 小弟我的绘图想实现实时绘图也就是图像会向左平移,为啥小弟我的程序实现不了,大家帮帮小弟我
  详细解决方案

小弟我的绘图想实现实时绘图也就是图像会向左平移,为啥小弟我的程序实现不了,大家帮帮小弟我

热度:9   发布时间:2016-05-05 03:50:19.0
我的绘图想实现实时绘图也就是图像会向左平移,为啥我的程序实现不了,大家帮帮我,在线等
 private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            int h = pictureBox1.Height;
            graph = e.Graphics;
            pictureBox1.BackColor = Color.Black;
            for (int 线 = 0; 线 < 100; 线 = 线 + 10)
            {
                graph.DrawLine(线条, new Point(线, 0), new Point(线, 100));//竖线需X坐标=同  
                graph.DrawLine(线条1, new Point(0, 线), new Point(200, 线));//横线需Y坐标=同  
                graph.DrawLine(线条, new Point(线 + 100, 0), new Point(线 + 100, 100));//竖线需X坐标=同  
            }
            for (int i = 0; i < 9; i++)
            {
                
                graph.DrawLine(p, i * 10, h - shuzu[i], (i+1) *10, h-shuzu[i+1]);

            }
         
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int len = 10;
            for (int i = 0; i < len; i++)
            {
                //判断数组越界
                if (i < len - 1)
                {
                    shuzu[i] = shuzu[i + 1];
                }
                else
                {
                    shuzu[len - 1] = rmd.Next(0,100);   
                } 
            }
            Invalidate();
        }  
    }
------解决思路----------------------
把 timer1_Tick 中除 Invalidate(); 外的代码移到  pictureBox1_Paint 开始处。这样便于检查错误
我试了一下,没有问题的(当然我把不相关的代码都去掉了)
        private int[] shuju = new int[10];
        private Random rand = new Random();

        public Form1()
        {
            InitializeComponent();

            for (int i = 0; i < shuju.Length; i++) shuju[i] = rand.Next(0, 100);
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            int i;
            for (i = 0; i < shuju.Length-1; i++)
            {
                 g.DrawLine(Pens.Green, new PointF(i * 10, shuju[i]), new PointF((i + 1) * 10, shuju[i + 1]));
            }
            for (i = 0; i < shuju.Length-1; i++) shuju[i] = shuju[i+1]; 
            shuju[i] = rand.Next(0, 100);
       }

不要是你的 timer1_Tick 没有被触发吧?
  相关解决方案