C#WinForm创建系统加载动画

思路:

1、系统执行(加载到内存)后,创建一个Form窗口用来显示动画

2、主程序页面初始化前,创建一个线程将添加动画并显示

3、主程序页面加载完毕,关闭用来显示动画的Form窗口

主要的三段代码:

private Form cartoonForm = new Form();


//系统加载动画
private void SystemLoadCartoon()
      {
            Thread LoadCartoon = new Thread(delegate ()
            {
                PictureBox pb = new PictureBox();
                //加载图片
                pb.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + "login.gif");
                //设置PictureBox大小
                pb.Width = pb.Image.Width;
                pb.Height = pb.Image.Height;
                //设置窗口大小
                cartoonForm.Width = pb.Width;
                cartoonForm.Height = pb.Height;
                //将PictureBox添加到Form里
                cartoonForm.Controls.Add(pb);
                //不显示Form的操作按钮,防止提前关闭窗口导致后面的invok方法报错
                cartoonForm.ControlBox = false;
                //显示窗口
                cartoonForm.ShowDialog();
             });
            LoadCartoon.Start();
        }



 //关闭加载动画
            cartoonForm.Invoke(new Action(() => {
                cartoonForm.Close();
            }));

完整片段:

 public partial class Form1 : Form
    {
        private Form cartoonForm = new Form();

        
        public Form1()
        {
            

            SystemLoadCartoon();

            InitializeComponent();
        }

        //系统加载动画
        private void SystemLoadCartoon()
        {
            Thread LoadCartoon = new Thread(delegate ()
            {
                PictureBox pb = new PictureBox();
                //加载图片
                pb.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + "login.gif");
                //设置PictureBox大小
                pb.Width = pb.Image.Width;
                pb.Height = pb.Image.Height;
                //设置窗口大小
                cartoonForm.Width = pb.Width;
                cartoonForm.Height = pb.Height;
                //将PictureBox添加到Form里
                cartoonForm.Controls.Add(pb);
                //不显示Form的操作按钮,防止提前关闭窗口导致后面的invok方法报错
                cartoonForm.ControlBox = false;
                //显示窗口
                cartoonForm.ShowDialog();
             });
            LoadCartoon.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            

            //关闭加载动画
            cartoonForm.Invoke(new Action(() => {
                cartoonForm.Close();
            }));
        }
}

 

你可能感兴趣的:(C#,c#)