Windows 表单启动画面 - 在加载主表单时显示表单

Windows Forms Splash Screen - Show a form while loading main form

我试图让初始屏幕首先出现,在初始屏幕之后出现 MainForm。但是我在启动画面中的进度条没有到达进度条的末尾。并且程序继续 运行 并且不起作用。

如何在加载主窗体时显示启动画面?

我的代码是这样的:

public partial class SplashForm : Form
{
    public SplashForm()
    { 
        InitializeComponent();
    }
    private void SplashForm_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
        timer1.Start();
        timer1.Interval = 1000;
        progressBar1.Maximum = 10;
        timer1.Tick += new EventHandler(timer1_Tick);
    }
    public void timer1_Tick(object sender, EventArgs e)
    {
        if (progressBar1.Value != 10)
        {
            progressBar1.Value++;
        }
        else
        {
            timer1.Stop();
            Application.Exit();
        }
    }     
}

下面是 MainForm 代码的第一部分:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        Application.Run(new SplashForm());
    }
}

创建启动画面的方法有多种:

  • 您可以依赖 WindowsFormsApplicationBase

  • 的启动画面功能
  • 您可以通过在不同的 UI 线程上显示表单并在主线程成功加载后隐藏它来自己实现该功能。

在此 post 中,我将展示这两种解决方案的示例。

Note: Those who are looking for showing a loading window or a loading gif animation during loading of data, can take a look at this post:

选项 1 - 使用 WindowsFormsApplicationBase 启动画面功能

  1. Microsoft.VisualBasic.dll 的引用添加到您的项目。
  2. 通过从 WindowsFormsApplicationBase
  3. 派生创建 MyApplication class
  4. 覆盖 OnCreateMainForm 并将您希望作为启动表单的来源分配给 MainForm 属性.
  5. 覆盖 OnCreateSplashScreen 并将要显示为初始屏幕的表单分配给 SplashScreen 属性.

  6. 在您的 Main 方法中,创建 MyApplication 的实例并调用其 Run 方法。

例子

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);
        var app = new MyApplication();
        app.Run(Environment.GetCommandLineArgs());
    }
}
public class MyApplication : WindowsFormsApplicationBase
{
    protected override void OnCreateMainForm()
    {
        MainForm = new YourMainForm();
    }
    protected override void OnCreateSplashScreen()
    {
        SplashScreen = new YourSplashForm();
    }
}

选项 2 - 使用不同的 UI 线程实现该功能

您可以通过在不同的 UI 线程中显示启动画面来自行实现该功能。为此,您可以在 Program class 中订阅主窗体的 Load 事件,并在那里显示和关闭启动画面。

例子

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

static class Program
{
    static Form SplashScreen;
    static Form MainForm;
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //Show Splash Form
        SplashScreen = new Form();
        var splashThread = new Thread(new ThreadStart(
            () => Application.Run(SplashScreen)));
        splashThread.SetApartmentState(ApartmentState.STA);
        splashThread.Start();

        //Create and Show Main Form
        MainForm = new Form8();
        MainForm.Load += MainForm_LoadCompleted;
        Application.Run(MainForm);
    }
    private static void MainForm_LoadCompleted(object sender, EventArgs e)
    {
        if (SplashScreen != null && !SplashScreen.Disposing && !SplashScreen.IsDisposed)
            SplashScreen.Invoke(new Action(() => SplashScreen.Close()));
        MainForm.TopMost = true;
        MainForm.Activate();
        MainForm.TopMost = false;
    }
}

Note: To show a smooth edge custom shaped splash screen take a look at this post: Windows Forms Transparent Background Image.