Windows窗体启动屏幕-加载主窗体时显示一个窗体

本文关键字:窗体 一个 显示 屏幕 启动 加载 Windows | 更新日期: 2023-09-27 18:11:17

我试图使闪屏首先出现,在闪屏之后,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());
    }
}

Windows窗体启动屏幕-加载主窗体时显示一个窗体

创建启动屏的方法有很多:

  • 可以依靠WindowsFormsApplicationBase的闪屏功能

  • 你可以通过在不同的UI线程上显示一个表单,并在main加载成功后隐藏它来显示自己实现这个特性。

在这篇文章中,我将展示一个两种解决方案的例子。

注意:那些正在寻找显示加载窗口或加载在其他线程加载数据时显示加载动画

选项1 -使用WindowsFormsApplicationBase启动屏幕功能

  1. 添加Microsoft.VisualBasic.dll的引用到你的项目。
  2. 创建MyApplication类,从WindowsFormsApplicationBase派生
  3. 重写OnCreateMainForm,并将你想要作为启动表单的from分配给MainForm属性。
  4. 覆盖OnCreateSplashScreen,并指定你想要显示为闪屏的形式到SplashScreen属性

  5. 在你的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类中主表单的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;
    }
}

注意:要显示平滑边缘自定义形状的启动屏幕,请查看这个帖子:Windows窗体透明背景形象。

多年来我的Winforms应用程序一直有一个亮点。飞溅持续5秒,非模态,但在顶部。初始化保持正常运行,没有问题。

在主程序中,我没有改变什么

    [STAThread]
    static void Main()
    {
        Application.SetHighDpiMode(HighDpiMode.SystemAware);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FrmMain());
    }
    //---------------------------------------------------------------------

这是我的splash,它有一个计时器,在5秒后关闭

    public partial class FrmSplash : Form
    {
      private Timer _timer;
      private int _cTick = 5;
      public FrmSplash()
      {
        InitializeComponent();
        _timer = new Timer() { Interval = 1000, Enabled = true };
        _timer.Tick += CTick;
      }
      private void CTick(object sender, EventArgs e)
      {
        if (--_cTick<0)
        {
            _timer.Enabled = false;
            Close();
        }
      }
   }

在主表单构造函数中,我在Application.DoEvents()之后调用splash,因此它将显示在屏幕上,同时我的初始化可以继续,

   //---------------------------------------------------------------------
   public FrmMain()
   {
      InitializeComponent();
      var  FrmSplash = new FrmSplash();
      FrmSplash.Show();
      Application.DoEvents();
   }