水如烟

                 顺其自然,水到渠成 LzmTW

文或代码皆是面向初学者.我是爱好者,也是初学者.那些"文章",只按自己理解写,我是不知术语名词的.所以只供参考,也仅供参考.

导航

How TO:自做AboutForm(.NET2005)

Posted on 2005-11-09 01:34  水如烟(LzmTW)  阅读(600)  评论(1编辑  收藏  举报
Author:水如烟 

     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

     下面是调用。新建一个工程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
     运行,结果如图: