我們在開發桌面應用程序的時候,由於程序啟動比較慢,往往為了提高用戶的體驗,增加一個閃屏,也就是SplashScreen,好處有:1、讓用戶看到加載的過程,提高程序的交互響應;2.可以簡短展示或者介紹程序的功能或者展示Logo,給客戶較深的印象。
本人在開發的共享軟件中,對於啟動比較慢的程序,也傾向於引入這個控件來展示下,先看看軟件啟動的時候的效果

中間的那些文字“正在初始化應用程序......”可以根據加載的進度顯示不同的內容,當然最好簡單扼要了,其他的內容你也可以視需要做相應變化,因為這個是一個Form,你想改變什么就改變什么的。
看看閃屏代碼如何使用先,首先我們在入口的Main函數中開始
static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //登陸界面 FrmLogin dlg = new FrmLogin(); dlg.StartPosition = FormStartPosition.CenterScreen; if (DialogResult.OK == dlg.ShowDialog()) { SplashScreen.Splasher.Show(typeof(SplashScreen.FrmSplashScreen)); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.Run(new FrmMain()); } } private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs ex) { //LogHelper.Error(ex.Exception); string message = string.Format("{0}\r\n操作發生錯誤,您需要退出系統么?", ex.Exception.Message); if (DialogResult.Yes ==MessageBox.Show(message,"系統錯誤",MessageBoxButtons.YesNo)) { Application.Exit(); } } }
上面代碼中:SplashScreen.Splasher.Show(typeof(SplashScreen.frmSplash));主要為啟動閃屏類代碼,
其中 Splasher 這個類當中使用后台線程,反射來實例化窗體,代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.Reflection; namespace SplashScreen { public class Splasher { private static Form m_SplashForm = null; private static ISplashForm m_SplashInterface = null; private static Thread m_SplashThread = null; private static string m_TempStatus = string.Empty; /// <summary> /// Show the SplashForm /// </summary> public static void Show(Type splashFormType) { if (m_SplashThread != null) return; if (splashFormType == null) { throw (new Exception("splashFormType is null")); } m_SplashThread = new Thread(new ThreadStart(delegate() { CreateInstance(splashFormType); Application.Run(m_SplashForm); })); m_SplashThread.IsBackground = true; m_SplashThread.SetApartmentState(ApartmentState.STA); m_SplashThread.Start(); } /// <summary> /// set the loading Status /// </summary> public static string Status { set { if (m_SplashInterface == null || m_SplashForm == null) { m_TempStatus = value; return; } m_SplashForm.Invoke( new SplashStatusChangedHandle(delegate(string str) { m_SplashInterface.SetStatusInfo(str); }), new object[] { value } ); } } /// <summary> /// Colse the SplashForm /// </summary> public static void Close() { if (m_SplashThread == null || m_SplashForm == null) return; try { m_SplashForm.Invoke(new MethodInvoker(m_SplashForm.Close)); } catch (Exception) { } m_SplashThread = null; m_SplashForm = null; } private static void CreateInstance(Type FormType) { //利用反射創建對象 object obj = Activator.CreateInstance(FormType); m_SplashForm = obj as Form; m_SplashInterface = obj as ISplashForm; if (m_SplashForm == null) { throw (new Exception("Splash Screen must inherit from System.Windows.Forms.Form")); } if (m_SplashInterface == null) { throw (new Exception("must implement interface ISplashForm")); } if (!string.IsNullOrEmpty(m_TempStatus)) m_SplashInterface.SetStatusInfo(m_TempStatus); } private delegate void SplashStatusChangedHandle(string NewStatusInfo); } }
為了增加啟動界面的說明,在啟動界面上增加了一個文字接口,可以在外部來修改啟動界面上的說明:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SplashScreen { /// <summary> /// interface for Splash Screen /// </summary> public interface ISplashForm { void SetStatusInfo(string NewStatusInfo); } }
然手在閃屏的窗體上繼承接口,並實現相關的接口代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SplashScreen { public partial class FrmSplashScreen : Form,ISplashForm { public FrmSplashScreen() { InitializeComponent(); } //實現接口方法,主要用於接口的反射調用 #region ISplashForm void ISplashForm.SetStatusInfo(string NewStatusInfo) { lbStatusInfo.Text = NewStatusInfo; } #endregion } }
然后程序在點擊“登錄”按鈕后,就可以在主界面上做閃屏界面等待了,frmMain窗體代碼:
namespace SplashScreen { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); System.Threading.Thread.Sleep(800); Splasher.Status = "正在展示相關的內容......"; System.Threading.Thread.Sleep(800); //.......此處加載耗時的代碼 Splasher.Status = "初始化完畢............"; System.Threading.Thread.Sleep(800); Splasher.Close(); } } }
