C# WinForm - 加载屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15836027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WinForm - loading screen
提问by CaTx
I would like to ask how to make a loading screen (just a picture or something) that appears while the program is being loaded, and disappears when the program has finished loading.
我想问一下如何制作一个加载画面(只是一张图片什么的),在程序加载时出现,程序加载完成后消失。
In fancier versions, I have seen the process bar (%) displayed. how can you have that, and how do you calculate the % to show on it?
在更高级的版本中,我看到了显示的进程栏 (%)。你怎么能拥有它,你如何计算显示在它上面的百分比?
I know there is a Form_Load() event, but I do not see a Form_Loaded() event, or the % as a property / attribute anywhere.
我知道有一个 Form_Load() 事件,但我没有看到 Form_Loaded() 事件,也没有看到 % 作为属性/属性在任何地方。
采纳答案by JSJ
all you need to create one form as splash screen and show it before you main start showing the landing page and close this splash once the landing page loaded.
您只需要创建一个表单作为启动画面并在主要开始显示登录页面之前显示它,并在登录页面加载后关闭此启动画面。
using System.Threading;
using System.Windows.Forms;
namespace MyTools
{
public class SplashForm : Form
{
//Delegate for cross thread call to close
private delegate void CloseDelegate();
//The type of form to be displayed as the splash screen.
private static SplashForm splashForm;
static public void ShowSplashScreen()
{
// Make sure it is only launched once.
if (splashForm != null) return;
splashForm = new SplashScreen();
Thread thread = new Thread(new ThreadStart(SplashForm.ShowForm));
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
static private void ShowForm()
{
if (splashForm != null) Application.Run(splashForm);
}
static public void CloseForm()
{
splashForm?.Invoke(new CloseDelegate(SplashForm.CloseFormInternal));
}
static private void CloseFormInternal()
{
if (splashForm != null)
{
splashForm.Close();
splashForm = null;
};
}
}
}
and the main program function looks like this:
主程序函数如下所示:
[STAThread]
static void Main(string[] args)
{
SplashForm.ShowSplashScreen();
MainForm mainForm = new MainForm(); //this takes ages
SplashForm.CloseForm();
Application.Run(mainForm);
}
Don't forget to add a form load event to your main form:
不要忘记向主表单添加表单加载事件:
private void MainForm_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Normal;
this.Focus(); this.Show();
}
It will bring the main form to the foreground after hiding the splash screen.
隐藏启动画面后,它将主窗体带到前台。
回答by TheJonz
If you're going to show the SplashForm more than once in your application, be sure to set the splashForm variable to null otherwise you'll get an error.
如果您要在应用程序中多次显示 SplashForm,请确保将 splashForm 变量设置为 null,否则您将收到错误消息。
static private void CloseFormInternal()
{
splashForm.Close();
splashForm = null;
}
回答by osexpert
I had problem with all other solutions I found, specially those that show splash in other thread than gui thread and specially on Citrix.
我发现的所有其他解决方案都有问题,特别是那些在 gui 线程以外的其他线程中显示飞溅的解决方案,特别是在 Citrix 上。
Example:
例子:
- Splash never closes
- Splash show on wrong monitor
- Splash show ok but mainform is showing behind all other windows
- 飞溅永远不会关闭
- 飞溅显示在错误的显示器上
- Splash 显示正常,但 mainform 显示在所有其他窗口后面
I ended up with this and it seems to work well.
我结束了这个,它似乎运作良好。
Splash form:
飞溅形式:
public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
Splash form cont:
飞溅形式续:
partial class Splash
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Splash));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(512, 224);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// Splash
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(512, 224);
this.ControlBox = false;
this.Controls.Add(this.pictureBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Splash";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Splash";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
}
Main:
主要的:
[STAThread]
static void Main(string[] _args)
{
ShowSplash();
MainForm mainForm = new MainForm();
Application.Run(mainForm);
}
private static void ShowSplash()
{
Splash sp = new Splash();
sp.Show();
Application.DoEvents();
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 1000;
t.Tick += new EventHandler((sender, ea) =>
{
sp.BeginInvoke(new Action(() =>
{
if (sp != null && Application.OpenForms.Count > 1)
{
sp.Close();
sp.Dispose();
sp = null;
t.Stop();
t.Dispose();
t = null;
}
}));
});
t.Start();
}