C#WinForm - 加载屏幕

CaT*_*aTx 11 c# screen loading form-load

我想问一下如何在程序加载时出现加载屏幕(只是图片或其他东西),并在程序加载完成后消失.

在发烧友的版本中,我看到了显示的进程条(%).你怎么能拥有它,你如何计算显示的百分比?

我知道有一个Form_Load()事件,但我没有看到Form_Loaded()事件,或者%作为属性/属性.

JSJ*_*JSJ 32

您需要创建一个表单作为启动画面,并在主开始显示登录页面之前显示它,并在加载登录页面后关闭此启动画面.

using System.Threading;
using System.Windows.Forms;

namespace MyTools
{
    public class SplashForm : Form
    {
        //Delegate for cross thread call to close
        private delegate void CloseDelegate();

        //The type of form to be displayed as the splash screen.
        private static SplashForm splashForm;

        static public void ShowSplashScreen()
        {
            // Make sure it is only launched once.

            if (splashForm != null)
                return;
            Thread thread = new Thread(new ThreadStart(SplashForm.ShowForm));
            thread.IsBackground = true;
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }

        static private void ShowForm()
        {
            splashForm = new SplashForm();
            Application.Run(splashForm);
        }

        static public void CloseForm()
        {
            splashForm.Invoke(new CloseDelegate(SplashForm.CloseFormInternal));
        }

        static private void CloseFormInternal()
        {
            splashForm.Close();
            splashForm = null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并且主程序功能如下所示:

[STAThread]
static void Main(string[] args)
{
    SplashForm.ShowSplashScreen();
    MainForm mainForm = new MainForm(); //this takes ages
    SplashForm.CloseForm();
    Application.Run(mainForm);
}
Run Code Online (Sandbox Code Playgroud)