进度栏winform中的动画栏

本文关键字:动画 winform | 更新日期: 2023-09-27 18:27:16

我目前正在编写一个扩展的进度条控件,100%开源,我已经创建了一些带有渐变和纯色的基本样式。

我想添加的选项之一是在栏中添加动画,很像windows 7和vista绿色进度栏。所以我需要在%栏中添加一个移动的"辉光",但我的尝试看起来很糟糕。

我的方法是画一个设定大小的椭圆,然后移动它的x位置,直到它到达动画再次开始时的终点。

首先,有没有人有任何链接或代码可以帮助我使用GDI或类似的方法来实现当前的Windows7发光效果?

我有几个其他的动画,也将添加到栏,因此GDI。

 private void renderAnimation(PaintEventArgs e)
    {
        if (this.AnimType == animoptions.Halo)
        {                
            Rectangle rec = e.ClipRectangle;
            Rectangle glow = new Rectangle();
            //SolidBrush brush = new SolidBrush(Color.FromArgb(100, Color.White));
            //int offset = (int)(rec.Width * ((double)Value / Maximum)) - 4;
            int offset = (int)(rec.Width / Maximum) * Value;
            if (this.animxoffset > offset)
            {
                this.animxoffset = 0;
            }
            glow.Height = rec.Height - 4;
            if (this.animxoffset + glow.X > offset)
            {
                glow.Width = offset - (this.animxoffset + 50);
            }
            else
            {
                glow.Width = 50;
            }

            glow.X = this.animxoffset;
            LinearGradientBrush brush = new LinearGradientBrush(glow, Color.FromArgb(0, Color.White), Color.FromArgb(100, Color.White), LinearGradientMode.Horizontal);
            e.Graphics.FillEllipse(brush, this.animxoffset, 2, glow.Width, glow.Height);
            brush.Dispose();
            string temp = offset.ToString();
            e.Graphics.DrawString(temp + " : " + glow.X.ToString(), DefaultFont, Brushes.Black, 2, 2);

            animTimer = new System.Timers.Timer();
            animTimer.Interval = 10;
            animTimer.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
            animTimer.Start();
        }
    } 
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        this.animTimer.Stop();
        this.animxoffset += 2;
        Invalidate();

    }

进度栏winform中的动画栏

这只是一个通过笔阵列迭代的辉光示例。您也可以使用透明图像(尽管它可能会对性能产生影响)。

        Pen[] gradient = { new Pen(Color.FromArgb(255, 200, 200, 255)), new Pen(Color.FromArgb(150, 200, 200, 255)), new Pen(Color.FromArgb(100, 200, 200, 255)) };
        int x = 20;
        int y = 20;
        int sizex = 200;
        int sizey = 10;
        int value = 25;

        //draw progress bar basic outline (position - 1 to compensate for the outline)
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(x-1, y-1, sizex, sizey));
        //draw the percentage done
        e.Graphics.FillRectangle(Brushes.AliceBlue, new Rectangle(x, y, (sizex/100)*value, sizey));
        //to add the glow effect just add lines around the area you want to glow.
        for (int i = 0; i < gradient.Length; i++)
        {
            e.Graphics.DrawRectangle(gradient[i], new Rectangle(x - (i + 1), y - (i + 1), (sizex / 100) * value + (2 * i), sizey + (2 * i)));
        }