咱們在開發桌面應用程序的時候,因爲程序啓動比較慢,每每爲了提升用戶的體驗,增長一個閃屏,也就是SplashScreen,好處有:一、讓用戶看到加載的過程,提升程序的交互響應;2.能夠簡短展現或者介紹程序的功能或者展現Logo,給客戶較深的印象。函數
本人在開發的共享軟件中,對於啓動比較慢的程序,也傾向於引入這個控件來展現下,先看看軟件啓動的時候的效果spa
中間的那些文字「正在初始化應用程序......」能夠根據加載的進度顯示不一樣的內容,固然最好簡單扼要了,其餘的內容你也能夠視須要作相應變化,由於這個是一個Form,你想改變什麼就改變什麼的。線程
看看閃屏代碼如何使用先,首先咱們在入口的Main函數中開始code
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));主要爲啓動閃屏類代碼,orm
其中 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); } }
爲了增長啓動界面的說明,在啓動界面上增長了一個文字接口,能夠在外部來修改啓動界面上的說明:blog
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(); } } }