HOW TO:溅射屏幕(Splash Screen),也叫程序启动画面的制作(.NET2003)
Posted on 2005-10-31 20:40 水如烟(LzmTW) 阅读(8635) 评论(1) 编辑 收藏 举报
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()里头。
类:
使用:(SplashScreenForm我还是随便用一个Form来代替)
看到有人提起,就想动手做做。可能要花两天时间,因为时间是零零碎碎的。
哎,很多常用的东西,在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.Forms
Imports System.Threading
Public MustInherit Class SplashScreenApplicationContextClass
Inherits System.Windows.Forms.ApplicationContext
Private _SplashScreenForm As Form
Private _SplashScreenTimer As System.Timers.Timer
Private _SplashScreenTimerInterVal As Integer = 5000
Private _MainFormFinshLoad As Boolean = False
Private _MainFormWindowState As Windows.Forms.FormWindowState
Private _CloseSplashScreen As Boolean = False
Private Delegate Sub DisposeDelegate()
Protected WriteOnly Property SplashScreenForm() As Form
Set(ByVal Value As Form)
Me._SplashScreenForm = Value
End Set
End Property
Protected WriteOnly Property SecondsShow() As Integer
Set(ByVal Value As Integer)
If Value <> 0 Then
Me._SplashScreenTimerInterVal = 1000 * Value
End If
End Set
End Property
Sub New()
Me.ShowSplashScreen()
Me.MainFormLoad()
End Sub
Private Sub DoEvents()
Application.DoEvents()
End Sub
Protected 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 = False
Dim DisplaySpashScreenThread As New Thread(New ThreadStart(AddressOf Me.DisplaySplashScreen))
DisplaySpashScreenThread.Start()
End Sub
Private Sub DisplaySplashScreen()
Me._SplashScreenTimer.Enabled = True
Application.Run(Me._SplashScreenForm)
End Sub
Private Sub SplashScreenDisplayTimeUp(ByVal sender As System.Object, ByVal e As System.timers.ElapsedEventArgs)
Me._SplashScreenTimer.Dispose()
Me._SplashScreenTimer = Nothing
Me._CloseSplashScreen = True
End Sub
Private 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 Sub
Private Sub MainFormLoadingDone(ByVal sender As Object, ByVal e As EventArgs)
RemoveHandler Me.MainForm.Load, New EventHandler(AddressOf Me.MainFormLoadingDone)
Do While Not Me._CloseSplashScreen
Me.DoEvents()
Loop
Me.HideSplashScreen()
End Sub
Private Sub HideSplashScreen()
MainFormActivate()
'先激活主窗体再关闭启动窗体,是为了保证程序为当前活动程序
Dim SplashScreenFormDisposeDelegate As DisposeDelegate = New DisposeDelegate(AddressOf Me._SplashScreenForm.Dispose)
Me._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate)
Me._SplashScreenForm = Nothing
End Sub
Private Sub MainFormActivate()
If _MainFormWindowState = FormWindowState.Minimized Then _MainFormWindowState = FormWindowState.Normal
If Me.MainForm.WindowState <> _MainFormWindowState Then Me.MainForm.WindowState = _MainFormWindowState
Me.MainForm.Activate()
End Sub
End Class
Imports System.Threading
Public MustInherit Class SplashScreenApplicationContextClass
Inherits System.Windows.Forms.ApplicationContext
Private _SplashScreenForm As Form
Private _SplashScreenTimer As System.Timers.Timer
Private _SplashScreenTimerInterVal As Integer = 5000
Private _MainFormFinshLoad As Boolean = False
Private _MainFormWindowState As Windows.Forms.FormWindowState
Private _CloseSplashScreen As Boolean = False
Private Delegate Sub DisposeDelegate()
Protected WriteOnly Property SplashScreenForm() As Form
Set(ByVal Value As Form)
Me._SplashScreenForm = Value
End Set
End Property
Protected WriteOnly Property SecondsShow() As Integer
Set(ByVal Value As Integer)
If Value <> 0 Then
Me._SplashScreenTimerInterVal = 1000 * Value
End If
End Set
End Property
Sub New()
Me.ShowSplashScreen()
Me.MainFormLoad()
End Sub
Private Sub DoEvents()
Application.DoEvents()
End Sub
Protected 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 = False
Dim DisplaySpashScreenThread As New Thread(New ThreadStart(AddressOf Me.DisplaySplashScreen))
DisplaySpashScreenThread.Start()
End Sub
Private Sub DisplaySplashScreen()
Me._SplashScreenTimer.Enabled = True
Application.Run(Me._SplashScreenForm)
End Sub
Private Sub SplashScreenDisplayTimeUp(ByVal sender As System.Object, ByVal e As System.timers.ElapsedEventArgs)
Me._SplashScreenTimer.Dispose()
Me._SplashScreenTimer = Nothing
Me._CloseSplashScreen = True
End Sub
Private 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 Sub
Private Sub MainFormLoadingDone(ByVal sender As Object, ByVal e As EventArgs)
RemoveHandler Me.MainForm.Load, New EventHandler(AddressOf Me.MainFormLoadingDone)
Do While Not Me._CloseSplashScreen
Me.DoEvents()
Loop
Me.HideSplashScreen()
End Sub
Private Sub HideSplashScreen()
MainFormActivate()
'先激活主窗体再关闭启动窗体,是为了保证程序为当前活动程序
Dim SplashScreenFormDisposeDelegate As DisposeDelegate = New DisposeDelegate(AddressOf Me._SplashScreenForm.Dispose)
Me._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate)
Me._SplashScreenForm = Nothing
End Sub
Private Sub MainFormActivate()
If _MainFormWindowState = FormWindowState.Minimized Then _MainFormWindowState = FormWindowState.Normal
If Me.MainForm.WindowState <> _MainFormWindowState Then Me.MainForm.WindowState = _MainFormWindowState
Me.MainForm.Activate()
End Sub
End Class
使用:(SplashScreenForm我还是随便用一个Form来代替)
Public Class App
Public Shared Sub Main()
Dim t As New MyContext
Application.Run(t)
End Sub
End Class
Public Class MyContext
Inherits SplashScreenApplicationContextClass
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = New Form2
End Sub
Protected Overrides Sub OnCreateSplashScreenForm()
Me.SplashScreenForm = New Form1
End Sub
Protected Overrides Sub SetSeconds()
Me.SecondsShow = 3 '显示3秒,若是0则取默认值5秒
End Sub
End Class
Public Shared Sub Main()
Dim t As New MyContext
Application.Run(t)
End Sub
End Class
Public Class MyContext
Inherits SplashScreenApplicationContextClass
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = New Form2
End Sub
Protected Overrides Sub OnCreateSplashScreenForm()
Me.SplashScreenForm = New Form1
End Sub
Protected Overrides Sub SetSeconds()
Me.SecondsShow = 3 '显示3秒,若是0则取默认值5秒
End Sub
End Class