Windows 窗体启动画面 - 在加载主窗体时显示窗体 [英] Windows Forms Splash Screen - Show a form while loading main form

查看:39
本文介绍了Windows 窗体启动画面 - 在加载主窗体时显示窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的代码是这样的:

public partial class SplashForm : Form{公共 SplashForm(){初始化组件();}私有无效SplashForm_Load(对象发送者,EventArgs e){timer1.Enabled = true;timer1.Start();timer1.Interval = 1000;progressBar1.Maximum = 10;timer1.Tick += new EventHandler(timer1_Tick);}public void timer1_Tick(对象发送者,EventArgs e){如果 (progressBar1.Value != 10){progressBar1.Value++;}别的{timer1.Stop();Application.Exit();}}}

这是MainForm的第一部分代码:

公共部分类 MainForm : Form{公共主窗体(){初始化组件();Application.Run(new SplashForm());}}

解决方案

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

  • 您可以依赖 WindowsFormsApplicationBase

  • 您可以通过在不同的 UI 线程上显示一个表单并在 main 成功加载后隐藏它来自己实现该功能.

在这篇文章中,我将展示这两种解决方案的示例.

<块引用>

注意:那些正在寻找显示加载窗口或加载加载数据时的gif动画,可以看看这个帖子:在其他线程中显示加载数据时的加载动画>

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

  1. Microsoft.VisualBasic.dll 的引用添加到您的项目中.
  2. 通过从 WindowsFormsApplicationBase
  3. 覆盖OnCreateMainForm 并将您想要作为启动表单的 from 分配给 MainForm 属性.
  4. 覆盖OnCreateSplashScreen 并将要显示为启动画面的表单分配给 SplashScreen 属性.

  5. 在你的 Main 方法中,创建一个 MyApplication 的实例并调用它的 Run 方法.

示例

使用系统;使用 System.Windows.Forms;使用 Microsoft.VisualBasic.ApplicationServices;静态类程序{///<总结>///应用程序的主要入口点.///</总结>[STAThread]静态无效主(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(true);var app = new MyApplication();app.Run(Environment.GetCommandLineArgs());}}公共类 MyApplication:WindowsFormsApplicationBase{受保护的覆盖 void OnCreateMainForm(){MainForm = new YourMainForm();}protected override void OnCreateSplashScreen(){SplashScreen = new YourSplashForm();}}

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

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

示例

使用系统;使用 System.Threading;使用 System.Windows.Forms;静态类程序{静态表单启动画面;静态表单主表单;[STAThread]静态无效主(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);//显示飞溅表单SplashScreen = new Form();var splashThread = new Thread(new ThreadStart(() =>Application.Run(SplashScreen)));splashThread.SetApartmentState(ApartmentState.STA);splashThread.Start();//创建并显示主窗体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 窗体透明背景图片.

I am trying to make the splash screen appears first and after the splash, the MainForm appears. But the progress bar which I have in splash screen don't get to the end of the bar. And the program continues running and not works.

How can I show the splash screen during loading the main form?

My code It's something like that :

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();
        }
    }     
}

Here are the first part of the code of the MainForm:

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

解决方案

There are different ways of creating splash screens:

  • You can rely on the splash screen feature of WindowsFormsApplicationBase

  • You can show implement the feature yourself by showing a form on a different UI thread and hiding it after the main from loaded successfully.

In this post I'll show an example of both solutions.

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: Show Loading animation during loading data in other thread

Option 1 - Use WindowsFormsApplicationBase Splash Screen feature

  1. Add a reference of Microsoft.VisualBasic.dll to your project.
  2. Create a MyApplication class by deriving from WindowsFormsApplicationBase
  3. override OnCreateMainForm and assign the from that you want to be the startup form to MainForm property.
  4. Override OnCreateSplashScreen and assign the form that you want to show as splash screen to SplashScreen property.

  5. In your Main method, create an instance of MyApplication and call its Run method.

Example

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();
    }
}

Option 2 - Implement the feature using a different UI thread

You can implement the feature yourself by showing the splash screen in a different UI thread. To do so, you can subscribe to Load event of the main form in Program class, and show and close your splash screen there.

Example

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.

这篇关于Windows 窗体启动画面 - 在加载主窗体时显示窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆