Windows窗体初始屏幕-加载主窗体时显示窗体 [英] Windows Forms Splash Screen - Show a form while loading main form
问题描述
我试图首先显示启动屏幕,并在启动后显示 MainForm
。但是我在初始屏幕中显示的进度条没有到达进度条的末尾。并且程序会继续运行并且无法运行。
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?
我的代码就像这样:
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());
}
}
推荐答案
有有多种创建启动屏幕的方法:
There are different ways of creating splash screens:
您可以通过在其他UI线程上显示表单并隐藏来自己显示实现功能
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.
注意:那些在数据加载过程中显示加载窗口或加载
gif动画的人可以看看这篇文章:显示在其他线程中加载数据期间的加载动画
选项1-使用WindowsFormsApplicationBase闪屏功能
- 添加一个
Microsoft.VisualBasic.dll
对项目的引用。 - 创建
MyApplication
WindowsFormsApplicationBase
- 覆盖
OnCreateMainForm
并分配所需的成为MainForm
属性的启动表单。 -
覆盖
OnCreateSplashScreen
并将要显示为初始屏幕的表单分配给SplashScreen
属性。
- Add a reference of
Microsoft.VisualBasic.dll
to your project. - Create a
MyApplication
class by deriving fromWindowsFormsApplicationBase
- override
OnCreateMainForm
and assign the from that you want to be the startup form toMainForm
property. Override
OnCreateSplashScreen
and assign the form that you want to show as splash screen toSplashScreen
property.
在您的 Main
方法中,创建 MyApplication
的实例并调用其运行
方法。
In your Main
method, create an instance of MyApplication
and call its Run
method.
示例
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
事件,并显示并关闭启动画面
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.
示例
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窗体透明背景
图像。
这篇关于Windows窗体初始屏幕-加载主窗体时显示窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!