由于MSDN中文文档还没出来,不晓得那些名词怎叫法,所以不说多了,贴代码和图象(我将操作的步骤一并说了)。
MS2005自带的SplashScreenForm少了一项功能,就是主窗体在加载项目时若时间较长,不能把信息显示在启动窗体中。我主要的想实现这个。
项目涉及到线程消息的传递,这方面我不懂。功能是实现了,为什么那样做,我也不明白。好了,开始。
首先建一个工程LzmTW.ApplicationBase,输出为Class Libary。我的SplashScreenForm好简单。如图:
上面项目中有两个关于About的文件,在另一文中介绍。
文件SplashScreenMainFormLoadingItemHandler.vb只是定义一个委托。就一行:
文件SplashScreenMainFormLoadingClass.vb:
下面看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添加代码。
效果如图。
好了,全说完了。不足之处,敬请指出。
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:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class SplashScreenForm Class SplashScreenForm
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(SplashScreenForm))
Me.ApplicationTitleLabel = New System.Windows.Forms.Label
Me.VersionLabel = New System.Windows.Forms.Label
Me.CopyRightLabel = New System.Windows.Forms.Label
Me.LoadingLabel = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'ApplicationTitleLabel
'
Me.ApplicationTitleLabel.BackColor = System.Drawing.Color.Transparent
Me.ApplicationTitleLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Bold)
Me.ApplicationTitleLabel.ForeColor = System.Drawing.SystemColors.ActiveCaption
Me.ApplicationTitleLabel.Location = New System.Drawing.Point(12, 77)
Me.ApplicationTitleLabel.Name = "ApplicationTitleLabel"
Me.ApplicationTitleLabel.Size = New System.Drawing.Size(369, 29)
Me.ApplicationTitleLabel.TabIndex = 0
Me.ApplicationTitleLabel.Text = "ApplicationTitle"
Me.ApplicationTitleLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'VersionLabel
'
Me.VersionLabel.AutoSize = True
Me.VersionLabel.BackColor = System.Drawing.Color.Transparent
Me.VersionLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.VersionLabel.ForeColor = System.Drawing.SystemColors.ActiveCaption
Me.VersionLabel.Location = New System.Drawing.Point(40, 176)
Me.VersionLabel.Name = "VersionLabel"
Me.VersionLabel.Size = New System.Drawing.Size(101, 15)
Me.VersionLabel.TabIndex = 1
Me.VersionLabel.Text = "版本: {0}.{1:00}"
'
'CopyRightLabel
'
Me.CopyRightLabel.AutoSize = True
Me.CopyRightLabel.BackColor = System.Drawing.Color.Transparent
Me.CopyRightLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.CopyRightLabel.ForeColor = System.Drawing.SystemColors.ActiveCaption
Me.CopyRightLabel.Location = New System.Drawing.Point(40, 191)
Me.CopyRightLabel.Name = "CopyRightLabel"
Me.CopyRightLabel.Size = New System.Drawing.Size(72, 15)
Me.CopyRightLabel.TabIndex = 2
Me.CopyRightLabel.Text = "CopyRight"
'
'LoadingLabel
'
Me.LoadingLabel.AutoSize = True
Me.LoadingLabel.BackColor = System.Drawing.Color.Transparent
Me.LoadingLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LoadingLabel.ForeColor = System.Drawing.SystemColors.ActiveCaption
Me.LoadingLabel.Location = New System.Drawing.Point(39, 220)
Me.LoadingLabel.Name = "LoadingLabel"
Me.LoadingLabel.Size = New System.Drawing.Size(59, 15)
Me.LoadingLabel.TabIndex = 3
Me.LoadingLabel.Text = "Loading"
'
'SplashScreenForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
Me.ClientSize = New System.Drawing.Size(393, 241)
Me.ControlBox = False
Me.Controls.Add(Me.LoadingLabel)
Me.Controls.Add(Me.CopyRightLabel)
Me.Controls.Add(Me.VersionLabel)
Me.Controls.Add(Me.ApplicationTitleLabel)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "SplashScreenForm"
Me.ShowInTaskbar = False
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Protected WithEvents ApplicationTitleLabel As System.Windows.Forms.Label
Protected WithEvents VersionLabel As System.Windows.Forms.Label
Protected WithEvents CopyRightLabel As System.Windows.Forms.Label
Protected WithEvents LoadingLabel As System.Windows.Forms.Label
End Class
Partial Class SplashScreenForm Class SplashScreenForm
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(SplashScreenForm))
Me.ApplicationTitleLabel = New System.Windows.Forms.Label
Me.VersionLabel = New System.Windows.Forms.Label
Me.CopyRightLabel = New System.Windows.Forms.Label
Me.LoadingLabel = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'ApplicationTitleLabel
'
Me.ApplicationTitleLabel.BackColor = System.Drawing.Color.Transparent
Me.ApplicationTitleLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Bold)
Me.ApplicationTitleLabel.ForeColor = System.Drawing.SystemColors.ActiveCaption
Me.ApplicationTitleLabel.Location = New System.Drawing.Point(12, 77)
Me.ApplicationTitleLabel.Name = "ApplicationTitleLabel"
Me.ApplicationTitleLabel.Size = New System.Drawing.Size(369, 29)
Me.ApplicationTitleLabel.TabIndex = 0
Me.ApplicationTitleLabel.Text = "ApplicationTitle"
Me.ApplicationTitleLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'VersionLabel
'
Me.VersionLabel.AutoSize = True
Me.VersionLabel.BackColor = System.Drawing.Color.Transparent
Me.VersionLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.VersionLabel.ForeColor = System.Drawing.SystemColors.ActiveCaption
Me.VersionLabel.Location = New System.Drawing.Point(40, 176)
Me.VersionLabel.Name = "VersionLabel"
Me.VersionLabel.Size = New System.Drawing.Size(101, 15)
Me.VersionLabel.TabIndex = 1
Me.VersionLabel.Text = "版本: {0}.{1:00}"
'
'CopyRightLabel
'
Me.CopyRightLabel.AutoSize = True
Me.CopyRightLabel.BackColor = System.Drawing.Color.Transparent
Me.CopyRightLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.CopyRightLabel.ForeColor = System.Drawing.SystemColors.ActiveCaption
Me.CopyRightLabel.Location = New System.Drawing.Point(40, 191)
Me.CopyRightLabel.Name = "CopyRightLabel"
Me.CopyRightLabel.Size = New System.Drawing.Size(72, 15)
Me.CopyRightLabel.TabIndex = 2
Me.CopyRightLabel.Text = "CopyRight"
'
'LoadingLabel
'
Me.LoadingLabel.AutoSize = True
Me.LoadingLabel.BackColor = System.Drawing.Color.Transparent
Me.LoadingLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LoadingLabel.ForeColor = System.Drawing.SystemColors.ActiveCaption
Me.LoadingLabel.Location = New System.Drawing.Point(39, 220)
Me.LoadingLabel.Name = "LoadingLabel"
Me.LoadingLabel.Size = New System.Drawing.Size(59, 15)
Me.LoadingLabel.TabIndex = 3
Me.LoadingLabel.Text = "Loading"
'
'SplashScreenForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
Me.ClientSize = New System.Drawing.Size(393, 241)
Me.ControlBox = False
Me.Controls.Add(Me.LoadingLabel)
Me.Controls.Add(Me.CopyRightLabel)
Me.Controls.Add(Me.VersionLabel)
Me.Controls.Add(Me.ApplicationTitleLabel)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "SplashScreenForm"
Me.ShowInTaskbar = False
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Protected WithEvents ApplicationTitleLabel As System.Windows.Forms.Label
Protected WithEvents VersionLabel As System.Windows.Forms.Label
Protected WithEvents CopyRightLabel As System.Windows.Forms.Label
Protected WithEvents LoadingLabel As System.Windows.Forms.Label
End Class
文件SplashScreenForm.vb:
Public Class SplashScreenForm Class SplashScreenForm
Private Delegate Sub SetTextCallback()Sub SetTextCallback(ByVal [text] As String)
Private WithEvents MainFormLoadingClass As New SplashScreenMainFormLoadingClass
Protected Overridable Sub SplashScreenForm_Load()Sub SplashScreenForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If My.Application.Info.Title <> "" Then
ApplicationTitleLabel.Text = My.Application.Info.Title
Else
ApplicationTitleLabel.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
End If
VersionLabel.Text = System.String.Format(VersionLabel.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor)
CopyRightLabel.Text = My.Application.Info.Copyright
LoadingLabel.Text = ""
End Sub
Private Sub OnLoadingItemChanged()Sub OnLoadingItemChanged(ByVal sender As Object, ByVal Message As String) Handles MainFormLoadingClass.SplshScreenFormEvent
If Me.LoadingLabel.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetLoadingLableText)
Me.Invoke(d, New Object() {Message})
Else
SetLoadingLableText(Message)
End If
End Sub
Private Sub SetLoadingLableText()Sub SetLoadingLableText(ByVal [text] As String)
Me.LoadingLabel.Text = [text]
End Sub
End Class
Private Delegate Sub SetTextCallback()Sub SetTextCallback(ByVal [text] As String)
Private WithEvents MainFormLoadingClass As New SplashScreenMainFormLoadingClass
Protected Overridable Sub SplashScreenForm_Load()Sub SplashScreenForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If My.Application.Info.Title <> "" Then
ApplicationTitleLabel.Text = My.Application.Info.Title
Else
ApplicationTitleLabel.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
End If
VersionLabel.Text = System.String.Format(VersionLabel.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor)
CopyRightLabel.Text = My.Application.Info.Copyright
LoadingLabel.Text = ""
End Sub
Private Sub OnLoadingItemChanged()Sub OnLoadingItemChanged(ByVal sender As Object, ByVal Message As String) Handles MainFormLoadingClass.SplshScreenFormEvent
If Me.LoadingLabel.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetLoadingLableText)
Me.Invoke(d, New Object() {Message})
Else
SetLoadingLableText(Message)
End If
End Sub
Private Sub SetLoadingLableText()Sub SetLoadingLableText(ByVal [text] As String)
Me.LoadingLabel.Text = [text]
End Sub
End Class
将项目编译后,得到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添加代码。
Public Class Form1 Class Form1
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SendMessage As New LzmTW.ApplicationBase.SplashScreenMainFormLoadingClass
For i As Integer = 999 To 0 Step -1
SendMessage.SendItemMessage(Nothing, String.Format("稍等,加载{0}中", i))
Next
SendMessage.SendItemMessage(Nothing, String.Format("稍等,正在加载{0}", "数据集"))
Dim b As New DataSet
'.
SendMessage.SendItemMessage(Nothing, "加载完毕")
'停顿2秒,看看
System.Threading.Thread.Sleep(2000)
End Sub
End Class
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SendMessage As New LzmTW.ApplicationBase.SplashScreenMainFormLoadingClass
For i As Integer = 999 To 0 Step -1
SendMessage.SendItemMessage(Nothing, String.Format("稍等,加载{0}中", i))
Next
SendMessage.SendItemMessage(Nothing, String.Format("稍等,正在加载{0}", "数据集"))
Dim b As New DataSet
'.
SendMessage.SendItemMessage(Nothing, "加载完毕")
'停顿2秒,看看
System.Threading.Thread.Sleep(2000)
End Sub
End Class
效果如图。
好了,全说完了。不足之处,敬请指出。