C#中闪屏问题如何解决

如何在c#中,建一个闪屛窗体,程序启动时,闪屏显示一段时间,自动关闭后,打开的是主窗体

可以使用以下方法在 C# 中创建闪屏窗体:

  • 创建一个新窗体,命名为 "SplashScreen"。
  • 在窗体的 Load 事件中设置定时器,指定闪屏窗体显示的时间。
  • 在定时器的 Tick 事件中关闭闪屏窗体,并打开主窗体。

示例代码如下:

public partial class SplashScreen : Form
{
    private Timer timer = new Timer();

    public SplashScreen()
    {
        InitializeComponent();

        timer.Interval = 5000; // 5 秒
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        timer.Stop();
        this.Close();
        MainForm mainForm = new MainForm();
        mainForm.Show();
    }
}

在主程序中只需要 new 一个 SplashScreen 窗口就可以了,具体代码如下

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new SplashScreen());
}

在这里我们设置了窗体在5s后关闭,并且打开了MainForm窗体,可以根据需要调整时间。