static int iW, iInterval, i,k,n;        private void button1_Click_1(object sender, EventArgs e)
        {
            
            iInterval = 3;
            i = iInterval;
            iW = 10;
            k = 0;
            n = 0;
            timer2.Interval = 100;
            this.timer2.Start();        }        private void timer2_Tick(object sender, EventArgs e)
        {
            if (i < iW)
            {
                if (k == 0)
                    twinraster(Color.White, Color.Black,Color.Red, i, 0, 0, this.pictureBox1.Width, 0, iW);
                else if(k==1)
                    twinraster(Color.Red, Color.White, Color.Black, i, 0, 0, this.pictureBox1.Width, 0, iW);
                else
                    twinraster(Color.Black, Color.Red, Color.White, i, 0, 0, this.pictureBox1.Width, 0, iW);                i += iInterval;
            }
            else
            {
                i = i - iW + 1;
                if (k == 0)
                {
                    k = 1;
                    n = 0;
                    return;
                }
                if (k == 1)
                {
                    if (n == 0)
                        k = 2;
                    else
                        k = 0;
                    return;
                }
                if (k == 2)
                {
                    k = 1;
                    n = 1;
                }
                //twinraster(Color.White, Color.Black, i, 0, 0, this.pictureBox1.Width, 0, iW);
                
            }
        }private void twinraster(Color cColor1, Color cColor2,Color cColor3, int ist, int iX1, int iY1, int iX2, int iY2, int iWidth)//画三色光栅
        {
            Pen pen1 = new Pen(cColor1, iWidth);
            Pen pen2 = new Pen(cColor2, iWidth);
            Pen pen3 = new Pen(cColor1, ist);
            Pen pen4 = new Pen(cColor3, iWidth);            Graphics myGraphics = this.pictureBox1.CreateGraphics();            myGraphics.DrawLine(pen3, iX1, 0, iX2, 0);            iY1 = ist;
            iY2 = ist;            for (; iY1 <= this.pictureBox1.Height; iY1 += iWidth, iY2 += iWidth)
            {
                myGraphics.DrawLine(pen2, iX1, iY1, iX2, iY2);
                iY1 += iWidth;
                iY2 += iWidth;
                myGraphics.DrawLine(pen4, iX1, iY1, iX2, iY2);
                iY1 += iWidth;
                iY2 += iWidth;
                myGraphics.DrawLine(pen1, iX1, iY1, iX2, iY2);
            }
            myGraphics.DrawLine(pen2, iX1, iY1, iX2, iY2);
        }我是这样写的,但是算法的效率肯定很差,而且在移动的时候,有一个地方会出错,颜色没控制得好,我已经弄了好久都没解决这个问题。主要是自己的算法已经一塌糊涂了,望知道的朋友帮忙看一下,谢谢了

解决方案 »

  1.   

    参考如下代码
    private void timer1_Tick(object sender, EventArgs e)
    {
        yOffset = (yOffset + speed) % (lineHeight * lineColors.Length);
        pictureBox1.Refresh();
    }private void button1_Click(object sender, EventArgs e)
    {
        timer2.Enabled = !timer2.Enabled;
    }/// <summary>
    /// 线的高度
    /// </summary>
    private int lineHeight = 10;
    /// <summary>
    /// 线条的颜色
    /// </summary>
    private Color[] lineColors = new Color[] { 
        Color.White, Color.Black, Color.Red, Color.Green };
    /// <summary>
    /// 横向偏移
    /// </summary>
    private int yOffset = 0;
    /// <summary>
    /// 滚动的速度
    /// </summary>
    private int speed = 2;/// <summary>
    /// 绘制全部线条
    /// </summary>
    /// <param name="AGraphics">绘制的画布</param>
    private void DrawFace(Graphics AGraphics)
    {
        int vIndex = 0; // 向下绘制
        for (int y = yOffset; y < AGraphics.ClipBounds.Height; y += lineHeight)
        {
            AGraphics.FillRectangle(new SolidBrush(lineColors[vIndex]),
                new RectangleF(0, y, AGraphics.ClipBounds.Width, lineHeight));
            vIndex = (vIndex + 1) % lineColors.Length;
        }    vIndex = lineColors.Length - 1; // 向上绘制
        for (int y = yOffset - lineHeight; y >= -lineHeight; y -= lineHeight)
        {
            vIndex = (vIndex + 1) % lineColors.Length;
            AGraphics.FillRectangle(new SolidBrush(
                lineColors[lineColors.Length - vIndex - 1]),
                new RectangleF(0, y, AGraphics.ClipBounds.Width, lineHeight));
        }
    }private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        DrawFace(e.Graphics);
    }