首页 > 学院 > 开发设计 > 正文

给程序添加启动画面

2019-11-17 02:55:56
字体:
来源:转载
供稿:网友
给程序添加启动画面

如果程序在装载时需要进行较长时间的处理,最好使用启动画面,一方面美化程序,一方面可以不使用户面对着一片空白的程序界面。我手头上一个小项目主界面启动时需要检查用户文件及运行环境是否有效,需要一段时间处理,因此想到要添加一个启动画面,在网上搜了一阵,发现下面两个方案:1、用C#给程序加启动画面并只允许一个应用程序实例运行http://www.zahui.com/html/14/36790.htm2、HOW TO:溅射屏幕(Splash Screen),也叫程序启动画面的制作(.NET2003)http://lzmtw.VEVb.com/archive/2005/10/31/265782.html第一个方案在实现与界面分离上做得不够好,启动界面(一个窗体)依赖于特定窗体,主窗体还必须添加一个PReLoad方法完成装载任务,只能在代码级重用。而且那个只允许一个实例的写法也太....第二个方案框架很好,但细微处理可能存在一点问题,需要判断主窗体的WindowState,整个代码也较复杂。我改动了一下,基本结构仿照第二个方案。功能:为程序添加启动界面,显示启动界面的同时加载主窗体,主窗体加载完毕后关闭启动界面,显示主窗体。启动画面停留的时间是设定的时间和主窗体装载所需时间两个的最大值。启动画面在另一个线程上运行。plus:我的水平还很差,见笑。程序代码如下://启动窗体虚基类,继承自applicationContextusing System.Windows.Forms;using System.Threading;using System;//启动画面虚基类,启动画面会停留一段时间,该时间是设定的时间和主窗体构造所需时间两个的最大值public abstract class SplashScreenApplicationContext : ApplicationContext{private Form _SplashScreenForm;//启动窗体private Form _PrimaryForm;//主窗体private System.Timers.Timer _SplashScreenTimer;private int _SplashScreenTimerInterVal = 5000;//默认是启动窗体显示5秒private bool _bSplashScreenClosed = false;private delegate void DisposeDelegate();//关闭委托,下面需要使用控件的Invoke方法,该方法需要这个委托public SplashScreenApplicationContext(){this.ShowSplashScreen();//这里创建和显示启动窗体this.MainFormLoad();//这里创建和显示启动主窗体}protected abstract void OnCreateSplashScreenForm();protected abstract void OnCreateMainForm();protected abstract void SetSeconds();protected Form SplashScreenForm{set{this._SplashScreenForm = value;}}protected Form PrimaryForm{//在派生类中重写OnCreateMainForm方法,在MainFormLoad方法中调用OnCreateMainForm方法//,在这里才会真正调用Form1(主窗体)的构造函数,即在启动窗体显示后再调用主窗体的构造函数//,以避免这种情况:主窗体构造所需时间较长,在屏幕上许久没有响应,看不到启动窗体set{this._PrimaryForm = value;}}protected int SecondsShow{//未设置启动画面停留时间时,使用默认时间set{if (value != 0){this._SplashScreenTimerInterVal = 1000 * value;}}}private void ShowSplashScreen(){this.SetSeconds();this.OnCreateSplashScreenForm();this._SplashScreenTimer = new System.Timers.Timer(((double)(this._SplashScreenTimerInterVal)));_SplashScreenTimer.Elapsed += new System.Timers.ElapsedEventHandler(new System.Timers.ElapsedEventHandler(this.SplashScreenDisplayTimeUp));this._SplashScreenTimer.AutoReset = false;Thread DisplaySpashScreenThread = new Thread(new ThreadStart(DisplaySplashScreen));DisplaySpashScreenThread.Start();}private void DisplaySplashScreen(){this._SplashScreenTimer.Enabled = true;Application.Run(this._SplashScreenForm);}private void SplashScreenDisplayTimeUp(object sender, System.Timers.ElapsedEventArgs e){this._SplashScreenTimer.Dispose();this._SplashScreenTimer = null;this._bSplashScreenClosed = true;}private void MainFormLoad(){this.OnCreateMainForm();while (!(this._bSplashScreenClosed)){Application.DoEvents();}DisposeDelegate SplashScreenFormDisposeDelegate = new DisposeDelegate(this._SplashScreenForm.Dispose );this._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate);this._SplashScreenForm = null;//必须先显示,再激活,否则主窗体不能在启动窗体消失后出现this._PrimaryForm.Show();this._PrimaryForm.Activate();this._PrimaryForm.Closed += new EventHandler(_PrimaryForm_Closed);}private void _PrimaryForm_Closed(object sender, EventArgs e){base.ExitThread();}}使用方法:定义一个启动类,应用程序从启动类启动,该类会使用继承自启动窗体虚基类的一个启动窗体类,在该类中定义启动窗体和主窗体。启动窗体和主窗体的代码略去,注意要删除机器生成的窗体代码的Main方法部分。public class StartUpClass{[STAThread]static void Main(){Application.Run(new mycontext());}}//启动窗体类(继承自启动窗体虚基类),启动画面会停留一段时间,该时间是设定的时间和主窗体构造所需时间两个的最大值public class mycontext : SplashScreenApplicationContext{protected override void OnCreateSplashScreenForm(){this.SplashScreenForm = new FormStart();//启动窗体}protected override void OnCreateMainForm(){this.PrimaryForm = new FormMain();//主窗体}protected override void SetSeconds(){this.SecondsShow = 2;//启动窗体显示的时间(秒)}}程序在vs2003下调试通过HOW TO:溅射屏幕(Splash Screen),也叫程序启动画面的制作(.NET2003)Posted on 2005-10-31 20:40水如烟(LzmTW)阅读(1800)评论(1)编辑收藏网摘所属分类:n、HOW TO系列-->Author:水如烟(LzmTW) 看到有人提起,就想动手做做。可能要花两天时间,因为时间是零零碎碎的。 哎,很多常用的东西,在2005版已经有了。当初看到好多人写基于2003的控件,这2005一出哪,全都没用了。心血白费了。这微软未免也太缺德了。应该说,2003是.NET的试用版,到了2005才算是正式版吧。或许基于2005的自写控件、组件寿命会长一些。否则,不骂娘才怪。 现在写写,也算是练练习吧。 溅射屏幕,即SplashScreenForm,有以下特点: 首先,程序是主窗体程序; 其次,SplashScreenForm在主窗体加载完毕后退出。 一般来说,SplashScreenForm比较简洁,窗体的内容只是显示程序主题、版权等信息;复杂些的,显示主程序的加载项目情况。 微软的SplashScreenForm向来简洁,Adobe的复杂些,就画窗体也花了一番心思。以下做的,以微软的作标准。 按照功能实现与界面分离的原则,这个类当然不能依赖于某个特定的SplashScreenForm或MainForm。参考了.NET2005的做法,继承了System.Windows.Forms.ApplicationContext做了一个SplashScreenApplicationContextClass基类。具体使用时,再继承这个类赋与特定参数,交Application.Run(ApplicationContext)来运行。因为基础知识不好,好多东西只知道用而不知为什么这样用,免不了的会出现这样那样的问题,请指正。 总体思路是:溅射屏幕显示的同时加载主窗体,主窗体加载完毕后关闭溅射屏幕,程序交给主窗体。溅射屏幕显示的时间由两个因素决定,一个是事前设定的溅射屏幕显示的时间,一个是主窗体加载所需要的时间,实际显示时间是两者中的最大值。 SplashScreenApplicationContextClass为什么要做为基类设计并要实现?主要考虑到主窗体在初始化时就需要时间,Sub New()是需要时间的,并非所有的程序都将初始事项放到Load()里头。类:Imports System.Windows.FormsImports System.ThreadingPublic MustInherit Class SplashScreenApplicationContextClassInherits System.Windows.Forms.ApplicationContextPrivate _SplashScreenForm As FormPrivate _SplashScreenTimer As System.Timers.TimerPrivate _SplashScreenTimerInterVal As Integer = 5000Private _MainFormFinshLoad As Boolean = FalsePrivate _MainFormWindowState As Windows.Forms.FormWindowStatePrivate _CloseSplashScreen As Boolean = FalsePrivate Delegate Sub DisposeDelegate()Protected WriteOnly Property SplashScreenForm() As FormSet(ByVal Value As Form)Me._SplashScreenForm = ValueEnd SetEnd PropertyProtected WriteOnly Property SecondsShow() As IntegerSet(ByVal Value As Integer)If Value <> 0 ThenMe._SplashScreenTimerInterVal = 1000 * ValueEnd IfEnd SetEnd PropertySub New()Me.ShowSplashScreen()Me.MainFormLoad()End SubPrivate Sub DoEvents()Application.DoEvents()End SubProtected MustOverride Sub OnCreateSplashScreenForm()Protected MustOverride Sub OnCreateMainForm()Protected MustOverride Sub SetSeconds()Private Sub ShowSplashScreen()Me.SetSeconds()Me.OnCreateSplashScreenForm()Me._SplashScreenTimer = New System.Timers.Timer(CType(Me._SplashScreenTimerInterVal, Double))AddHandler _SplashScreenTimer.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Me.SplashScreenDisplayTimeUp)Me._SplashScreenTimer.AutoReset = FalseDim DisplaySpashScreenThread As New Thread(New ThreadStart(AddressOf Me.DisplaySplashScreen))DisplaySpashScreenThread.Start()End SubPrivate Sub DisplaySplashScreen()Me._SplashScreenTimer.Enabled = TrueApplication.Run(Me._SplashScreenForm)End SubPrivate Sub SplashScreenDisplayTimeUp(ByVal sender As System.Object, ByVal e As System.timers.ElapsedEventArgs)Me._SplashScreenTimer.Dispose()Me._SplashScreenTimer = NothingMe._CloseSplashScreen = TrueEnd SubPrivate Sub MainFormLoad()Me.OnCreateMainForm()_MainFormWindowState = Me.MainForm.WindowState '保存主窗体状态Me.MainForm.WindowState = FormWindowState.Normal '非Normal情形下,主窗体会Show出来AddHandler Me.MainForm.Load, New EventHandler(AddressOf Me.MainFormLoadingDone)End SubPrivate Sub MainFormLoadingDone(ByVal sender As Object, ByVal e As EventArgs)RemoveHandler Me.MainForm.Load, New EventHandler(AddressOf Me.MainFormLoadingDone)Do While Not Me._CloseSplashScreenMe.DoEvents()LoopMe.HideSplashScreen()End SubPrivate Sub HideSplashScreen()MainFormActivate()'先激活主窗体再关闭启动窗体,是为了保证程序为当前活动程序Dim SplashScreenFormDisposeDelegate As DisposeDelegate = New DisposeDelegate(AddressOf Me._SplashScreenForm.Dispose)Me._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate)Me._SplashScreenForm = NothingEnd SubPrivate Sub MainFormActivate()If _MainFormWindowState = FormWindowState.Minimized Then _MainFormWindowState = FormWindowState.NormalIf Me.MainForm.WindowState <> _MainFormWindowState Then Me.MainForm.WindowState = _MainFormWindowStateMe.MainForm.Activate()End SubEnd Class使用:(SplashScreenForm我还是随便用一个Form来代替)Public Class AppPublic Shared Sub Main()Dim t As New MyContextApplication.Run(t)End SubEnd ClassPublic Class MyContextInherits SplashScreenApplicationContextClassProtected Overrides Sub OnCreateMainForm()Me.MainForm = New Form2End SubProtected Overrides Sub OnCreateSplashScreenForm()Me.SplashScreenForm = New Form1End SubProtected Overrides Sub SetSeconds()Me.SecondsShow = 3 '显示3秒,若是0则取默认值5秒End SubEnd Class


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表