由于MSDN中文文档还没出来,不晓得那些名词怎叫法,所以不说多了,贴代码和图象(我将操作的步骤一并说了)。
MS2005自带的SplashScreenForm少了一项功能,就是主窗体在加载项目时若时间较长,不能把信息显示在启动窗体中。我主要的想实现这个。
项目涉及到线程消息的传递,这方面我不懂。功能是实现了,为什么那样做,我也不明白。好了,开始。
首先建一个工程LzmTW.ApplicationBase,输出为Class Libary。我的SplashScreenForm好简单。如图:
上面项目中有两个关于About的文件,在另一文中介绍。
文件SplashScreenMainFormLoadingItemHandler.vb只是定义一个委托。就一行:
Public Delegate Sub SplashScreenMainFormLoadingItemHandler(ByVal sender As Object, ByVal Message As String)
文件SplashScreenMainFormLoadingClass.vb:
Public Class SplashScreenMainFormLoadingClass
Protected Friend Shared Event SplshScreenFormEvent As SplashScreenMainFormLoadingItemHandler
Private IsFirst As Boolean = True
Public Sub SendItemMessage(ByVal sender As Object, ByVal Message As String)
System.Threading.Thread.Sleep(0)
If IsFirst Then
RaiseEvent SplshScreenFormEvent(Nothing, Nothing)
IsFirst = False
End If
RaiseEvent SplshScreenFormEvent(sender, Message)
End Sub
End Class
Protected Friend Shared Event SplshScreenFormEvent As SplashScreenMainFormLoadingItemHandler
Private IsFirst As Boolean = True
Public Sub SendItemMessage(ByVal sender As Object, ByVal Message As String)
System.Threading.Thread.Sleep(0)
If IsFirst Then
RaiseEvent SplshScreenFormEvent(Nothing, Nothing)
IsFirst = False
End If
RaiseEvent SplshScreenFormEvent(sender, Message)
End Sub
End Class
下面看SplashScreenForm,它由三个文件组成。单击Solution Explorer的Show All Files按钮。显示如下:
资源文件SplashScreenForm.resx这里不用管它。
文件SplashScreenForm.designer.vb:
文件SplashScreenForm.vb:
将项目编译后,得到LzmTW.ApplicationBase.dll。下面是如何使用了。
新建一个工程,选Windows Application。如下图:
添加一个新项目,当然是指SplashScreenForm。这里选取Inherited Form,如图:
单击Add,下一步单击Brow定位选取LzmTW.ApplicationBase.dll,出来一个项目选取对话框,如图:
选取SpashScreenForm然后单OK。这样,工程多了一个Form2,如图:
接下来是工程部署,怎么显示这个SplashScreen了。我还是将一些细节性的交待一番。说那么多,是基于我假定你也刚接触MS2005。
双击(或右键Open)My Project,选取Application的Splash Screen为Form2,如图:
现在可以运行程序了。可以看到SplashScreen显示2秒后再出来Form1主窗体。.NET2.0的WindowsFormsApplicationBase设置SplashScreen显示时间默认为2000毫秒。
2秒的显示时间不足以让我来截图。下面,说说怎么重设它的显示时间。我改为20秒,方便截图。
一样的Show All Files,打开My Project下Application.Designer.vb,在OnCreateSplashScreen()下添加代码:Me.MinimumSplashScreenDisplayTime = 20000,如图:
现在重新运行程序,可以截到启动画面了。
下面说说怎么显示主窗体的加载信息。
先删去Me.MinimumSplashScreenDisplayTime = 20000那行代码。现在启动窗体的显示时间由主窗体加载项目的时间来决定了,当然,下面的做法令加载的时间超过2秒。右键点击Form1,选View Code.在Form1_Load添加代码。
效果如图。
好了,全说完了。不足之处,敬请指出。