我想问一下如何在加载程序时出现加载屏幕(只是图片或其他内容),并在程序加载完成后消失。
在发烧友版本中,我看到显示了进程条(%)。你怎么能拥有它,你如何计算显示在它上面的百分比?
我知道有一个Form_Load()事件,但我没有看到Form_Loaded()事件,或者%as作为属性/属性。
答案 0 :(得分:31)
您需要创建一个表单作为启动画面,并在主开始显示登录页面之前显示它,并在加载登录页面后关闭此启动画面。
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;
Thread thread = new Thread(new ThreadStart(SplashForm.ShowForm));
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
static private void ShowForm()
{
splashForm = new SplashForm();
Application.Run(splashForm);
}
static public void CloseForm()
{
splashForm.Invoke(new CloseDelegate(SplashForm.CloseFormInternal));
}
static private void CloseFormInternal()
{
splashForm.Close();
splashForm = null;
}
}
}
,主程序功能如下:
[STAThread]
static void Main(string[] args)
{
SplashForm.ShowSplashScreen();
MainForm mainForm = new MainForm(); //this takes ages
SplashForm.CloseForm();
Application.Run(mainForm);
}
答案 1 :(得分:1)
如果您要在应用程序中多次显示SplashForm,请确保将splashForm变量设置为null,否则您将收到错误。
static private void CloseFormInternal()
{
splashForm.Close();
splashForm = null;
}
答案 2 :(得分:0)
我遇到了所有其他解决方案的问题,特别是那些在gui线程以外的其他线程中显示的问题,特别是在Citrix上。
示例:
我最终得到了它,似乎运作良好。
启动表格:
public partial class Splash : Form
{
public Splash()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
飞溅形式续:
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;
}
主:
[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();
}
答案 3 :(得分:0)
这是在打开应用程序时制作加载屏幕的更简单的方法。
async private void Form1_Shown(object sender, EventArgs e)
{
pictureBox1.Visible = true;
await Task.Delay(2000);
pictureBox1.Visible = false;
}
答案 4 :(得分:0)
你也可以用这个...(表单的名字是“Home.cs”)
std::cv_status::no_timeout
在表单的属性中:await Task.Delay(10000);
this.Hide();
Home h1 = new Home();
h1.ShowDialog();