HOW TO:溅射屏幕(Splash Screen),也叫程序启动画面的制作(.NET2003)
在我的Blog上
http://lzmtw.cnblogs.com/archive/2005/10/31/265782.html
仅供参考.也请提意见.
问题点数:0、回复次数:25Top
1 楼lzmtw(水如烟)回复于 2005-11-01 21:34:49 得分 0
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 _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._SplashScreenTimer_Tick)
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 _SplashScreenTimer_Tick(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()
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()
Dim SplashScreenFormDisposeDelegate As DisposeDelegate = New DisposeDelegate(AddressOf Me._SplashScreenForm.Dispose)
Me._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate)
Me._SplashScreenForm = Nothing
Me.MainForm.Activate()
End Sub
End Class
Top
2 楼lzmtw(水如烟)回复于 2005-11-01 21:35:17 得分 0
使用:
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秒
End Sub
End ClassTop
3 楼tiaoci(我挑刺,我快乐)回复于 2005-11-02 09:16:40 得分 0
溅射屏幕(Splash Screen)
头一次听说那个叫 溅射屏幕~~~~,汗啊....
Top
4 楼lzmtw(水如烟)回复于 2005-11-02 12:01:19 得分 0
楼上,我写这个时查查什么叫Splash Screen,才晓得原来叫溅射屏幕~~~~,一样的汗啊....Top
5 楼wuyazhe(wyz&xyl)回复于 2005-11-02 12:26:04 得分 0
原来是这样。呵呵。Top
6 楼pzping(小虾)回复于 2005-11-02 14:00:45 得分 0
我试了一下,MAINFORM在NORMAL状态时可以,但MAINFORM状态设成最大化显示时,
SplashScreenForm 窗口显示不出来!
是程序有问题还是我设置有问题!Top
7 楼lzmtw(水如烟)回复于 2005-11-02 14:04:14 得分 0
不可能吧,一会我试试.
代码里可以看到,MainFormd在加载完之前是不会Show出来的.Top
8 楼lzmtw(水如烟)回复于 2005-11-02 14:09:07 得分 0
果是.谢谢,我细瞧瞧.Top
9 楼lzmtw(水如烟)回复于 2005-11-02 14:20:31 得分 0
我修改成这样,你帮忙看看还有什么问题:
Private _MainFormWindowState As Windows.Forms.FormWindowState
Private Sub MainFormLoad()
Me.OnCreateMainForm()
_MainFormWindowState = Me.MainForm.WindowState
Me.MainForm.WindowState = FormWindowState.Normal
AddHandler Me.MainForm.Load, New EventHandler(AddressOf Me.MainFormLoadingDone)
End Sub
Private Sub HideSplashScreen()
Dim SplashScreenFormDisposeDelegate As DisposeDelegate = New DisposeDelegate(AddressOf Me._SplashScreenForm.Dispose)
Me._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate)
Me._SplashScreenForm = Nothing
Me.MainForm.WindowState = _MainFormWindowState
Me.MainForm.Activate()
End SubTop
10 楼lzmtw(水如烟)回复于 2005-11-02 14:23:19 得分 0
原因应该是这样吧,WindowState一变动,就马上拥有焦点了.
如果上面的控件有变动吗?会不会如此?再试.Top
11 楼pzping(小虾)回复于 2005-11-02 14:37:03 得分 0
可以了,但是能不能让一开始MAINFORM就最大化显示,
现在要等SplashScreenForm 窗口完成后,MAINFORM才最大化动作,显得不好看!Top
12 楼lzmtw(水如烟)回复于 2005-11-02 14:44:36 得分 0
呵,这倒是.不过不知道有什么办法
难道先让它Visible=False,最大化后再True?哈Top
13 楼lzmtw(水如烟)回复于 2005-11-02 14:47:26 得分 0
不过一般的程序是取Normal,少有Max的.在Load或Initailize处,可以依据屏幕的分辨率设Size.Top
14 楼lzmtw(水如烟)回复于 2005-11-02 14:55:57 得分 0
在这窗体加如下代码可以达到最大化的效果了:
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mSize As Size = Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size
Me.Size = mSize
Me.Location = New Point(0, 0)
End SubTop
15 楼lzmtw(水如烟)回复于 2005-11-02 15:04:53 得分 0
呵,Size跟Location的次序是有讲究的.得先Location,再Size
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Location = New Point(0, 0)
Dim mSize As Size = Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size
Me.Size = mSize
End SubTop
16 楼lzmtw(水如烟)回复于 2005-11-02 15:09:07 得分 0
老天爷,哪有讲究.我的测试环境有问题.Top
17 楼lzmtw(水如烟)回复于 2005-11-02 15:28:52 得分 0
激活主窗体还是有问题Top
18 楼pupo(泡泡)回复于 2005-11-02 16:38:49 得分 0
通常叫闪屏吧。。溅射屏幕,想吓死人啊,呵呵Top
19 楼lzmtw(水如烟)回复于 2005-11-02 16:47:26 得分 0
呵,我也没闻嘛.我只知道是启动画面,哪知啥溅呀射呀的.听着确是吓人.
通常叫闪屏?我以前还听说叫LOGO的.Top
20 楼tiaoci(我挑刺,我快乐)回复于 2005-11-02 17:14:46 得分 0
一般就叫"启动屏"吧,Dotnet2.0 里面有个
WindowsFormsApplicationBase.SplashScreen
到时候看MS把这个翻译成什么,嘿嘿
Top
21 楼hamadou(闵峰--为了理想而奋斗)回复于 2005-11-02 22:19:51 得分 0
其实,这个的确叫溅射屏幕的,早在delphi里就已经有这个叫法了。呵呵!
Top
22 楼lzmtw(水如烟)回复于 2005-11-03 13:01:05 得分 0
Private Sub HideSplashScreen()
Dim SplashScreenFormDisposeDelegate As DisposeDelegate = New DisposeDelegate(AddressOf Me._SplashScreenForm.Dispose)
Me._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate)
Me._SplashScreenForm = Nothing
Me.MainForm.WindowState = _MainFormWindowState
Me.MainForm.Activate()
End Sub
改成了
Private Sub HideSplashScreen()
Me.MainForm.WindowState = _MainFormWindowState
Me.MainForm.Activate()
Dim SplashScreenFormDisposeDelegate As DisposeDelegate = New DisposeDelegate(AddressOf Me._SplashScreenForm.Dispose)
Me._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate)
Me._SplashScreenForm = Nothing
End Sub
保证程序为当前活动程序Top
23 楼AreZong()回复于 2005-11-03 15:36:49 得分 0
路过,我是来学习顺便捡点分的。Top
24 楼beijingbeerman(啤酒肚)回复于 2006-01-16 15:04:56 得分 0
upTop
25 楼cnwd()回复于 2006-01-21 11:42:44 得分 0
markTop




