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

时间:2023-09-09 00:13:47 

之前写了一个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动画实现代码

标签:Winform,圆形环绕,Loading动画
0
投稿

猜你喜欢

  • springboot 正确的在异步线程中使用request的示例代码

    2023-11-24 22:36:13
  • Android开源AndroidSideMenu实现抽屉和侧滑菜单

    2023-10-09 09:24:51
  • IDEA 2020 本土化,真的是全中文了(真香)

    2023-11-25 08:02:58
  • 利用Matlab复刻羊了个羊小游戏

    2021-10-10 17:13:05
  • C#网络编程中常用特性介绍

    2021-09-03 23:07:51
  • C语言 奇偶排序算法详解及实例代码

    2023-04-17 04:47:39
  • SpringBoot新手入门的快速教程

    2021-09-28 23:23:25
  • ES结合java代码聚合查询详细示例

    2022-08-31 01:23:29
  • android实现打地鼠游戏

    2023-09-25 08:45:59
  • c# HttpWebRequest通过代理服务器抓取网页内容应用介绍

    2023-04-04 20:10:35
  • C#之CLR内存原理初探

    2023-02-16 00:58:09
  • Java基于Javafaker生成测试数据

    2023-11-23 09:36:16
  • Java 实现RSA非对称加密算法

    2021-08-19 16:27:51
  • Mybatis整合达梦数据库的完整步骤记录

    2023-11-23 07:15:37
  • Java类成员访问权限控制知识总结

    2021-09-12 10:36:53
  • Android仿微信发送语音消息的功能及示例代码

    2021-07-06 19:31:39
  • Java设计模式之状态模式

    2022-05-08 07:24:25
  • idea 创建properties配置文件的步骤

    2023-11-18 18:03:30
  • Java编程实现非对称加密的方法详解

    2023-08-24 01:21:26
  • 详解Java异常处理中finally子句的运用

    2023-11-29 10:10:30
  • asp之家 软件编程 m.aspxhome.com