咱们在开发桌面应用程序的时候,因为程序启动比较慢,每每为了提升用户的体验,增长一个闪屏,也就是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(); } } }