欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Winform圆形环绕的Loading动画实现代码

程序员文章站 2024-03-31 12:33:40
之前写了一个wpf的圆形环绕的loading动画,现在写一个winform的圆形环绕的loading动画。 1.新建winform项目,添加一个picturebox控件,...

之前写了一个wpf的圆形环绕的loading动画,现在写一个winform的圆形环绕的loading动画。

1.新建winform项目,添加一个picturebox控件,命名为:picturebox;

2.引用中添加using system.drawing.drawing2d;

3.form窗体命名为:loading,cs全部代码如下:

复制代码 代码如下:

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.drawing.drawing2d;

namespace circle_processbar
{
    public partial class loading : form
    {
        private int count = -1;
        private arraylist images = new arraylist();
        public bitmap[] bitmap = new bitmap[8];
        private int _value = 1;
        private color _circlecolor = color.red;
        private float _circlesize = 0.8f;

        public loading()
        {
            initializecomponent();      
        }

        public color circlecolor
        {
            get { return _circlecolor; }
            set
            {
                _circlecolor = value;
                invalidate();
            }
        }

        public float circlesize
        {
            get { return _circlesize; }
            set
            {
                if (value <= 0.0f)
                    _circlesize = 0.05f;
                else
                    _circlesize = value > 4.0f ? 4.0f : value;
                invalidate();
            }
        }

        public bitmap drawcircle(int j)
        {
            const float angle = 360.0f / 8; bitmap map = new bitmap(150, 150);
            graphics g = graphics.fromimage(map);

            g.translatetransform(width / 2.0f, height / 2.0f);
            g.rotatetransform(angle * _value);
            g.interpolationmode = interpolationmode.highqualitybicubic;
            g.smoothingmode = smoothingmode.antialias;
            int[] a = new int[8] { 25, 50, 75, 100, 125, 150, 175, 200 };
            for (int i = 1; i <= 8; i++)
            {
                int alpha = a[(i + j - 1) % 8];
                color drawcolor = color.fromargb(alpha, _circlecolor);
                using (solidbrush brush = new solidbrush(drawcolor))
                {
                    float sizerate = 3.5f / _circlesize;
                    float size = width / (6 * sizerate);

                    float diff = (width / 10.0f) - size;

                    float x = (width / 80.0f) + diff;
                    float y = (height / 80.0f) + diff;
                    g.fillellipse(brush, x, y, size, size);
                    g.rotatetransform(angle);
                }
            }
            return map;
        }


        public void draw()
        {
            for (int j = 0; j < 8; j++)
            {
                bitmap[7-j] = drawcircle(j);
            }
        }
        protected override void onresize(eventargs e)
        {
            setnewsize();
            base.onresize(e);
        }

        protected override void onsizechanged(eventargs e)
        {
            setnewsize();
            base.onsizechanged(e);
        }

        private void setnewsize()
        {
            int size = math.max(width, height);
            size = new size(size, size);
        }

        public void set()
        {
            for (int i = 0; i < 8; i++)
            {
                draw();

                bitmap map = new bitmap((bitmap[i]), new size(120, 110));

                images.add(map);
            }
            picturebox.image = (image)images[0];
           picturebox.size = picturebox.image.size;

        }
        private void picturebox_click(object sender, eventargs e)
        {
            this.visible = false;
            base.dispose();
        }

        private void timer_tick(object sender, eventargs e)
        {
            set();
            count = (count + 1) % 8;
            picturebox.image = (image)images[count];

        }

        private void button1_click(object sender, eventargs e)
        {
            this.visible = false;
            base.dispose();
        }
    }
}


4.效果如图:

Winform圆形环绕的Loading动画实现代码