C#WinForm实现Loading等待界面

简介: 上篇博客中解决了程序加载时屏幕闪烁的问题。但是,加载的过程变得很缓慢。这个给用户的体验也不是很好,我这里想加一个Loading的进度条。项目启动的时候,加载进度条,界面UI加载完毕,进度条消失。

上篇博客中解决了程序加载时屏幕闪烁的问题。

但是,加载的过程变得很缓慢。

这个给用户的体验也不是很好,我这里想加一个Loading的进度条。

项目启动的时候,加载进度条,界面UI加载完毕,进度条消失。

文末有资源,可下载。

新建一个项目,添加一个窗体。窗体中添加一个pictureBox,添加Loading图片。

image.png

设置窗体属性

StartPosition :CenterScreen在屏幕中心显示

TopMost:True置顶显示

ShowInTaskbar:False不在任务栏显示

FormBorderStyle:None不显示窗体边框和标题栏

TransparencyKey:Control颜色为Control的部分透明

BackColor:Control窗体背景颜色设为Control

调用:

scss

复制代码

LoadingHelper.ShowLoadingScreen();//显示
LoadingHelper.CloseForm();//关闭

Loading窗体代码:

csharp

复制代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace loadingForm
{
    public partial class loading : Form
    {
        public loading()
        {
            InitializeComponent();
        }
        private void loading_Load(object sender, EventArgs e)
        {
            //this.BackColor = Color.Transparent;
            this.Opacity = 1;
        }
        /// <summary>
        /// 关闭命令
        /// </summary>
        public void closeOrder()
        {
            if (this.InvokeRequired)
            {
                //这里利用委托进行窗体的操作,避免跨线程调用时抛异常,后面给出具体定义
                CONSTANTDEFINE.SetUISomeInfo UIinfo = new CONSTANTDEFINE.SetUISomeInfo(new Action(() =>
                {
                    while (!this.IsHandleCreated)
                    {
                        ;
                    }
                    if (this.IsDisposed)
                        return;
                    if (!this.IsDisposed)
                    {
                        this.Dispose();
                    }
                }));
                this.Invoke(UIinfo);
            }
            else
            {
                if (this.IsDisposed)
                    return;
                if (!this.IsDisposed)
                {
                    this.Dispose();
                }
            }
        }
        private void loading_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!this.IsDisposed)
            {
                this.Dispose(true);
            }
        }
    }
    class CONSTANTDEFINE
    {
        public delegate void SetUISomeInfo();
    }
}

Loading类代码:LoadingHelper.cs

csharp

复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace loadingForm
{
    public class LoadingHelper
    {
        #region 相关变量定义
        /// <summary>
        /// 定义委托进行窗口关闭
        /// </summary>
        private delegate void CloseDelegate();
        private static loading loadingForm;
        private static readonly Object syncLock = new Object();  //加锁使用
        #endregion
        //private LoadingHelper()
        //{
        //}
        /// <summary>
        /// 显示loading框
        /// </summary>
        public static void ShowLoadingScreen()
        {
            // Make sure it is only launched once.
            if (loadingForm != null)
                return;
            Thread thread = new Thread(new ThreadStart(LoadingHelper.ShowForm));
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        /// <summary>
        /// 显示窗口
        /// </summary>
        private static void ShowForm()
        {
            if (loadingForm != null)
            {
                loadingForm.closeOrder();
                loadingForm = null;
            }
            loadingForm = new loading();
            loadingForm.TopMost = true;
            loadingForm.ShowDialog();
        }
        /// <summary>
        /// 关闭窗口
        /// </summary>
        public static void CloseForm()
        {
            Thread.Sleep(50); //可能到这里线程还未起来,所以进行延时,可以确保线程起来,彻底关闭窗口
            if (loadingForm != null)
            {
                lock (syncLock)
                {
                    Thread.Sleep(50);
                    if (loadingForm != null)
                    {
                        Thread.Sleep(50);  //通过三次延时,确保可以彻底关闭窗口
                        loadingForm.Invoke(new CloseDelegate(LoadingHelper.CloseFormInternal));
                    }
                }
            }
        }
        /// <summary>
        /// 关闭窗口,委托中使用
        /// </summary>
        private static void CloseFormInternal()
        {
            loadingForm.closeOrder();
            loadingForm = null;
        }
    }
}

原文链接:点击这里,走你

有好的建议,请在下方输入你的评论。

欢迎访问个人博客guanchao.site

欢迎访问我的小程序:打开微信->发现->小程序->搜索“时间里的”

目录
相关文章
|
2月前
|
SQL 数据库连接 应用服务中间件
C#WinForm基础编程(三)
C#WinForm基础编程
79 0
|
2月前
C#WinForm基础编程(二)
C#WinForm基础编程
61 0
|
2月前
|
C# 数据安全/隐私保护
C#WinForm基础编程(一)
C#WinForm基础编程
62 0
|
6月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
37 0
|
1月前
|
C# 开发者
35.c#:winform窗口
35.c#:winform窗口
13 1
|
2月前
|
C#
C# Winform 选择文件夹和选择文件
C# Winform 选择文件夹和选择文件
47 0
|
7月前
|
程序员 C# 索引
C#之二十 Win Form对话框
C#之二十 Win Form对话框
40 0
|
7月前
|
Java C# 索引
C#之 十九 使用WinForm控件
C#之 十九 使用WinForm控件
114 0
|
6月前
|
C#
C#之四十一 在Winform中从外部拖动节点到树形结构
C#之四十一 在Winform中从外部拖动节点到树形结构
42 0
|
1月前
|
C#
24. C# 编程:用户设定敌人初始血值的实现
24. C# 编程:用户设定敌人初始血值的实现
22 0