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

 更新时间:2014年01月22日 16:11:16   作者:  
这篇文章主要介绍了Winform圆形环绕的Loading动画实现代码,有需要的朋友可以参考一下

之前写了一个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.效果如图:

相关文章

  • C#中的yield关键字的使用方法介绍

    C#中的yield关键字的使用方法介绍

    yield这个关键字是和迭代器挂钩的,而且是与return一起以yield return的形式合用的,用来返回迭代器中的条目。
    2013-04-04
  • C#操作数据库总结(vs2005+sql2005)

    C#操作数据库总结(vs2005+sql2005)

    C#操作数据库总结,每次做项目都会用到数据库,对数据库的操作都是糊里糊涂从书里找代码用。通过昨天晚上与今天早上的努力,把数据库的操作整理了一下,下面把整理结果做个小结
    2012-09-09
  • C#创建一个小型Web Server(Socket实现)

    C#创建一个小型Web Server(Socket实现)

    这篇文章主要介绍了关于C#利用Socket实现创建一个小型Web Server的相关资料,文中通过示例代码介绍的很详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • C#深拷贝方法探究及性能比较(多种深拷贝)

    C#深拷贝方法探究及性能比较(多种深拷贝)

    这篇文章主要介绍了C#中使用NetCDF存储二维数据的读写操作简单应用,探究了以下几种C#对象深拷贝方式,同时简单对比了以下列出的几种深拷贝方式的速度,需要的朋友可以参考下
    2022-04-04
  • c# 获取计算机硬件信息的示例代码

    c# 获取计算机硬件信息的示例代码

    这篇文章主要介绍了c# 获取计算机硬件信息的示例代码,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
    2020-10-10
  • WPF ProgressBar实现实时进度效果

    WPF ProgressBar实现实时进度效果

    这篇文章主要介绍了WPF ProgressBar实现实时进度效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • C#实现的文件批量重命名功能示例

    C#实现的文件批量重命名功能示例

    这篇文章主要介绍了C#实现的文件批量重命名功能,结合具体实例形式分析了C#针对文件的遍历、属性修改相关操作技巧,需要的朋友可以参考下
    2017-07-07
  • c#单例模式(Singleton)的6种实现

    c#单例模式(Singleton)的6种实现

    这篇文章主要介绍了c#单例模式(Singleton)的6种实现 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C#写入对象或集合类型数据到xml文件的方法

    C#写入对象或集合类型数据到xml文件的方法

    这篇文章主要介绍了C#写入对象或集合类型数据到xml文件的方法,涉及C#针对XML文件的相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • C#操作XML文件实例汇总

    C#操作XML文件实例汇总

    这篇文章主要介绍了C#操作xml文件实例,包括了对XML文件节点的查找、遍历、删除、添加等。是C#程序设计中非常重要的技巧,需要的朋友可以参考下
    2014-08-08

最新评论