Author:水如烟
MS2005自带的About Box内容太少,觉得也不大好看。所以自做一个。这回我不说操作过程了,不清楚的话,可以参考我的另一文《HOW TO:溅射屏幕(Splash Screen),也叫程序启动画面的制作(.NET2005)》。界面如图:
文件AboutForm.Designer.vb代码:
文件AboutFormAddtoMainFormClass.vb:
下面是调用。新建一个工程TestAboutForm,输出为Windows Application.先运行一下,结果当然是这样的:
这次不再是通过添加Inherited Form新项目来调用这个AboutForm,而是直接引用LzmTW.ApplicationBase.Dll,在文件Application.Designer.vb添加代码
MS2005自带的About Box内容太少,觉得也不大好看。所以自做一个。这回我不说操作过程了,不清楚的话,可以参考我的另一文《HOW TO:溅射屏幕(Splash Screen),也叫程序启动画面的制作(.NET2005)》。界面如图:
文件AboutForm.Designer.vb代码:
代码
文件AboutFormAddtoMainFormClass.vb:
Imports System.Windows.Forms
Public Class AboutFormAddtoMainFormClass
Private Const TEXT_DECLARE As String = "关于 {0}"
Private Const PRODUCT_DECLARE As String = "{0} 版本 {1}" & vbCrLf & "{2}. 保留所有权利"
Private Const AUTHORIZATION_DECLARE As String = "{0}" & vbCrLf & "{1}"
Private _AboutForm As AboutForm
Private _MainForm As Windows.Forms.Form
Private _AboutToolStripMenuItem As ToolStripMenuItem
Public ReadOnly Property AboutToolStripMenuItem() As ToolStripMenuItem
Get
Return _AboutToolStripMenuItem
End Get
End Property
Sub New(ByVal MainForm As Windows.Forms.Form)
_MainForm = MainForm
Me.Initialize()
Me.AddMenuItemToMainFormMainMenu()
End Sub
Private Sub Initialize()
_AboutForm = New AboutForm
With My.Application.Info
_AboutForm.Text = String.Format(Me.TEXT_DECLARE, .Title)
_AboutForm.ProductLabel.Text = String.Format(Me.PRODUCT_DECLARE, .ProductName, .Version, .Copyright)
_AboutForm.UserTextBox.Text = String.Format(Me.AUTHORIZATION_DECLARE, System.Environment.UserName, System.Environment.MachineName)
_AboutForm.DescriptionTextBox.Text = .Description
End With
_AboutForm.ICOPictureBox.Image = CType(_MainForm.Icon.ToBitmap, System.Drawing.Image)
For Each RefAsm As Reflection.AssemblyName In Reflection.Assembly.GetEntryAssembly.GetReferencedAssemblies
_AboutForm.AssemblyListView.Items.Add(New System.Windows.Forms.ListViewItem(New String() {RefAsm.Name, RefAsm.Version.ToString}))
Next
AddHandler _AboutForm.SystemInfoButton.Click, AddressOf SystemInfoButton_Click
End Sub
Protected Overridable Sub SystemInfoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
System.Diagnostics.Process.Start("msinfo32.exe")
End Sub
Private Sub AddMenuItemToMainFormMainMenu()
Dim MenuStrip As MenuStrip = _MainForm.MainMenuStrip
If MenuStrip Is Nothing Then
MenuStrip = New MenuStrip
_MainForm.Controls.Add(MenuStrip)
_MainForm.MainMenuStrip = MenuStrip
End If
Dim HelpToolStripMenuItem As Windows.Forms.ToolStripMenuItem
Dim HelpToolStripItem() As System.Windows.Forms.ToolStripItem
HelpToolStripItem = MenuStrip.Items.Find("HelpToolStripMenuItem", False)
If HelpToolStripItem.Length = 0 Then
HelpToolStripMenuItem = New Windows.Forms.ToolStripMenuItem
HelpToolStripMenuItem.Name = "HelpToolStripMenuItem"
HelpToolStripMenuItem.Text = "帮助(&H)"
MenuStrip.Items.Add(HelpToolStripMenuItem)
Else
HelpToolStripMenuItem = HelpToolStripItem(0)
End If
Dim AboutToolStripItem() As System.Windows.Forms.ToolStripItem
AboutToolStripItem = HelpToolStripMenuItem.DropDownItems.Find("AboutToolStripMenuItem", False)
If AboutToolStripItem.Length = 0 Then
_AboutToolStripMenuItem = New Windows.Forms.ToolStripMenuItem
_AboutToolStripMenuItem.Name = "AboutToolStripMenuItem"
_AboutToolStripMenuItem.Text = String.Format("关于 {0}(&A)", My.Application.Info.Title)
HelpToolStripMenuItem.DropDownItems.Add(_AboutToolStripMenuItem)
AddHandler _AboutToolStripMenuItem.Click, AddressOf AboutToolStripMenuItem_Click
Else
_AboutToolStripMenuItem = AboutToolStripItem(0)
End If
End Sub
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If _AboutForm Is Nothing OrElse _AboutForm.IsDisposed Then
Me.Initialize()
End If
_AboutForm.ShowDialog(_MainForm)
End Sub
End Class
Public Class AboutFormAddtoMainFormClass
Private Const TEXT_DECLARE As String = "关于 {0}"
Private Const PRODUCT_DECLARE As String = "{0} 版本 {1}" & vbCrLf & "{2}. 保留所有权利"
Private Const AUTHORIZATION_DECLARE As String = "{0}" & vbCrLf & "{1}"
Private _AboutForm As AboutForm
Private _MainForm As Windows.Forms.Form
Private _AboutToolStripMenuItem As ToolStripMenuItem
Public ReadOnly Property AboutToolStripMenuItem() As ToolStripMenuItem
Get
Return _AboutToolStripMenuItem
End Get
End Property
Sub New(ByVal MainForm As Windows.Forms.Form)
_MainForm = MainForm
Me.Initialize()
Me.AddMenuItemToMainFormMainMenu()
End Sub
Private Sub Initialize()
_AboutForm = New AboutForm
With My.Application.Info
_AboutForm.Text = String.Format(Me.TEXT_DECLARE, .Title)
_AboutForm.ProductLabel.Text = String.Format(Me.PRODUCT_DECLARE, .ProductName, .Version, .Copyright)
_AboutForm.UserTextBox.Text = String.Format(Me.AUTHORIZATION_DECLARE, System.Environment.UserName, System.Environment.MachineName)
_AboutForm.DescriptionTextBox.Text = .Description
End With
_AboutForm.ICOPictureBox.Image = CType(_MainForm.Icon.ToBitmap, System.Drawing.Image)
For Each RefAsm As Reflection.AssemblyName In Reflection.Assembly.GetEntryAssembly.GetReferencedAssemblies
_AboutForm.AssemblyListView.Items.Add(New System.Windows.Forms.ListViewItem(New String() {RefAsm.Name, RefAsm.Version.ToString}))
Next
AddHandler _AboutForm.SystemInfoButton.Click, AddressOf SystemInfoButton_Click
End Sub
Protected Overridable Sub SystemInfoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
System.Diagnostics.Process.Start("msinfo32.exe")
End Sub
Private Sub AddMenuItemToMainFormMainMenu()
Dim MenuStrip As MenuStrip = _MainForm.MainMenuStrip
If MenuStrip Is Nothing Then
MenuStrip = New MenuStrip
_MainForm.Controls.Add(MenuStrip)
_MainForm.MainMenuStrip = MenuStrip
End If
Dim HelpToolStripMenuItem As Windows.Forms.ToolStripMenuItem
Dim HelpToolStripItem() As System.Windows.Forms.ToolStripItem
HelpToolStripItem = MenuStrip.Items.Find("HelpToolStripMenuItem", False)
If HelpToolStripItem.Length = 0 Then
HelpToolStripMenuItem = New Windows.Forms.ToolStripMenuItem
HelpToolStripMenuItem.Name = "HelpToolStripMenuItem"
HelpToolStripMenuItem.Text = "帮助(&H)"
MenuStrip.Items.Add(HelpToolStripMenuItem)
Else
HelpToolStripMenuItem = HelpToolStripItem(0)
End If
Dim AboutToolStripItem() As System.Windows.Forms.ToolStripItem
AboutToolStripItem = HelpToolStripMenuItem.DropDownItems.Find("AboutToolStripMenuItem", False)
If AboutToolStripItem.Length = 0 Then
_AboutToolStripMenuItem = New Windows.Forms.ToolStripMenuItem
_AboutToolStripMenuItem.Name = "AboutToolStripMenuItem"
_AboutToolStripMenuItem.Text = String.Format("关于 {0}(&A)", My.Application.Info.Title)
HelpToolStripMenuItem.DropDownItems.Add(_AboutToolStripMenuItem)
AddHandler _AboutToolStripMenuItem.Click, AddressOf AboutToolStripMenuItem_Click
Else
_AboutToolStripMenuItem = AboutToolStripItem(0)
End If
End Sub
Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If _AboutForm Is Nothing OrElse _AboutForm.IsDisposed Then
Me.Initialize()
End If
_AboutForm.ShowDialog(_MainForm)
End Sub
End Class
下面是调用。新建一个工程TestAboutForm,输出为Windows Application.先运行一下,结果当然是这样的:
这次不再是通过添加Inherited Form新项目来调用这个AboutForm,而是直接引用LzmTW.ApplicationBase.Dll,在文件Application.Designer.vb添加代码
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.TestAboutForm.Form1
'LzmTW 添加如下代码
Dim AboutForm As New LzmTW.ApplicationBase.AboutFormAddtoMainFormClass(Me.MainForm)
End Sub
运行,结果如图:Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.TestAboutForm.Form1
'LzmTW 添加如下代码
Dim AboutForm As New LzmTW.ApplicationBase.AboutFormAddtoMainFormClass(Me.MainForm)
End Sub