Main form
Код:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace SplashScreen
{
/// <summary>
/// Summary description for MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.StatusBar statusBar1;
// instance member to keep reference to splash form
private SplashForm frmSplash;
// delegate for the UI updater
public delegate void UpdateUIDelegate(bool IsDataLoaded);
//
// Some of the code was removed for simplicity
//
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
/// <summary>
/// Handles the Load event of the MainForm control.
/// </summary>
private void MainForm_Load(object sender, System.EventArgs e)
{
// Update UI
UpdateUI(false);
// Show the splash form
frmSplash = new SplashForm(this);
// Do some time consuming work in separate thread
Thread t = new Thread(new ThreadStart(DoSomeWork));
t.IsBackground = true;
t.Start();
}
/// <summary>
/// Time consuming method
/// </summary>
private void DoSomeWork()
{
// This is time consuming operation - loading data, etc.
System.Threading.Thread.Sleep(10000);
// Update UI
Invoke(new UpdateUIDelegate(UpdateUI), new object[] { true });
}
/// <summary>
/// Updates the UI.
/// </summary>
private void UpdateUI(bool IsDataLoaded)
{
if (IsDataLoaded)
{
this.statusBar1.Text = "Done.";
// close the splash form
if (this.frmSplash != null) {
frmSplash.Close();
}
}
else
{
this.statusBar1.Text = "Loading data ...";
}
}
}
}
Splash form
Код:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace SplashScreen
{
/// <summary>
/// Summary description for SplashForm.
/// </summary>
public class SplashForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
// instance member to keep a reference to main form
private Form MainForm;
// flag to indicate if the form has been closed
private bool IsClosed = false;
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SplashForm" /> class.
/// </summary>
public SplashForm()
{
InitializeComponent();
}
/// <summary>
/// Initializes a new instance of the <see cref="SplashForm" /> class.
/// </summary>
public SplashForm(Form mainForm):this() {
// Store the reference to parent form
MainForm = mainForm;
// Attach to parent form events
MainForm.Deactivate += new System.EventHandler(this.MainForm_Deactivate);
MainForm.Activated += new System.EventHandler(this.MainForm_Activated);
MainForm.Move += new System.EventHandler(this.MainForm_Move);
// Adjust appearance
this.ShowInTaskbar = false; // do not show form in task bar
this.TopMost = true; // show splash form on top of main form
this.StartPosition = FormStartPosition.Manual;
this.Visible = false;
// Adjust location
AdjustLocation();
}
#endregion
#region Methods
private void MainForm_Deactivate(object sender, EventArgs e)
{
if (!this.IsClosed)
{
this.Visible = false;
}
}
private void MainForm_Activated(object sender, EventArgs e)
{
if (!this.IsClosed)
{
this.Visible = true;
}
}
private void MainForm_Move(object sender, EventArgs e)
{
// Adjust location
AdjustLocation();
}
private void SplashForm_Closed(object sender, System.EventArgs e)
{
this.IsClosed = true;
}
private void AdjustLocation()
{
// Adjust the position relative to main form
int dx = (MainForm.Width - this.Width) / 2;
int dy = (MainForm.Height - this.Height) / 2;
Point loc = new Point(MainForm.Location.X, MainForm.Location.Y);
loc.Offset(dx, dy);
this.Location = loc;
}
#endregion
}
}