对于Splash Screen的改进

上一次谈到的Splash Screen解决方案可以很好地显示一个Splash Screen,并且通过Splash Screen上的Timer控件指定Splash Screen的显示时间。但是这个解决方案存在一个问题,即Splash Screen在显示过程中不能同时执行主窗体的初始化工作,即使代码这样写也不行:
ExpandedBlockStart.gif ContractedBlock.gif   Public   Sub New() Sub New()
InBlock.gif        
MyBase.New()
InBlock.gif
InBlock.gif        
Me.Cursor = Cursors.AppStarting
InBlock.gif
InBlock.gif        
Dim frm As New SplashForm
InBlock.gif        frm.FormBorderStyle 
= FormBorderStyle.None
InBlock.gif        frm.MaximizeBox 
= False
InBlock.gif        frm.MinimizeBox 
= False
InBlock.gif        frm.StartPosition 
= FormStartPosition.CenterScreen
InBlock.gif        frm.ControlBox 
= False
InBlock.gif        frm.ShowInTaskbar 
= False
InBlock.gif        frm.ShowDialog()
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
'该调用是 Windows 窗体设计器所必需的。
InBlock.gif
        InitializeComponent()
InBlock.gif
InBlock.gif        
'在 InitializeComponent() 调用之后添加任何初始化
InBlock.gif

InBlock.gif        
'
InBlock.gif
        '加载图书类型至TreeView
InBlock.gif
        PopulateBookType()
InBlock.gif
InBlock.gif                
Me.Cursor = Cursors.Default
ExpandedBlockEnd.gif    
End Sub

None.gif
如果SplashForm的Timer设定为5秒钟,那么在SplashForm显示的5钟内程序将停止在frm.ShowDialog()处,而不能执行下面的主窗体初始化代码。

一个比较好的解决方案就是使用Thread,这里SplashForm保持不变,需要增加一个SplashFormContainer类用以实现线程操作,显示和隐藏SplashForm,代码如下:
None.gif Imports  System.Threading
None.gif
' SplashForm包装类,用于实现
None.gif'
线程操作
None.gif'
显示/隐藏SplashForm
ExpandedBlockStart.gifContractedBlock.gif
Public   Class SplashFormContainer Class SplashFormContainer
InBlock.gif    
Private m_thr As Thread
InBlock.gif    
Private m_splashform As SplashForm
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Sub New()Sub New()
InBlock.gif        m_thr 
= New Thread(AddressOf Show)
InBlock.gif        m_thr.Start()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Private Sub Show()Sub Show()
InBlock.gif        m_splashform 
= New SplashForm
InBlock.gif        m_splashform.ShowDialog()
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub Close()Sub Close()
InBlock.gif        m_thr.
Join()
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class

另外主窗体需要作如下修改:
None.gif   Dim  splashContainer  As  SplashFormContainer
ExpandedBlockStart.gifContractedBlock.gif    
Public   Sub New() Sub New()
InBlock.gif        
MyBase.New()
InBlock.gif
InBlock.gif        
'显示Splash Screen
InBlock.gif
        Dim splashContainer = New SplashFormContainer
InBlock.gif
InBlock.gif        
'该调用是 Windows 窗体设计器所必需的。
InBlock.gif
        InitializeComponent()
InBlock.gif
InBlock.gif        
'在 InitializeComponent() 调用之后添加任何初始化
InBlock.gif

InBlock.gif        
'加载图书类型至TreeView
InBlock.gif
        PopulateBookType()
InBlock.gif
InBlock.gif        
Me.StartPosition = FormStartPosition.CenterScreen
InBlock.gif        Thread.Sleep(
1000)
InBlock.gif
InBlock.gif        
'关闭Splash Screen
InBlock.gif
        splashContainer.Close()
InBlock.gif
InBlock.gif        
Me.Cursor = Cursors.Default
ExpandedBlockEnd.gif    
End Sub


总结:
通过线程的使用可以实现在SplashForm显示的同时,完成窗体初始化的工作。
VB.Net 2005中提供了对Splash Screen的内置支持,看来有不少人遇到了同样的问题,并且Microsoft意识到了该问题并把它解决了。 emteeth.gif

转载于:https://www.cnblogs.com/chinapro/archive/2005/05/09/152059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值