WinForm設計之主窗體
阿新 • • 發佈:2019-01-25
Author:水如煙
Inherits LzmTW.uSystem.uWindows.uForms.uMainForm.MainForm
'實現特定選單載入ProtectedOverridesSub MenuManagerRead()
IfNot IO.File.Exists(Me.MenuManager.FileName) ThenMe.MenuManager.GetDefault() '預設選單Me.MenuManager.SaveFile()
ElseMe.MenuManager.ReadFile()
EndIfEnd SubEnd Class Me.Load
Dim mToolStripMenuItem As ToolStripMenuItem =CType(Me.MenuManager.FindToolStripItem("HideWorkspace"), ToolStripMenuItem)
If mToolStripMenuItem IsNotNothingThen
mToolStripMenuItem.PerformClick()
mToolStripMenuItem.Enabled =FalseEndIfEnd Sub
FriendClass OpenTxtFileService
Private gMainForm As LzmTW.uSystem.uWindows.uForms.uMainForm.MainForm
SubNew()
gMainForm =CType(My.Application.ApplicationContext.MainForm, LzmTW.uSystem.uWindows.uForms.uMainForm.MainForm)
Initialize()
End SubPrivateSub Initialize()
With gMainForm.WorkSpace.AddControlTo(LzmTW.uSystem.uWindows.uForms.MainFormSplitContainer.Panels.Center, New RichTextBox)
.Name ="Rich"
.Dock = DockStyle.Fill
EndWith'繫結主窗體[開啟]選單項單擊事件 gMainForm.MenuManager.AddActionHandler("Open", "Click", "OnOpenClick", Me)
'單擊事件也可以這樣繫結'gMainForm.MenuManager.AddClickHandler("Open", AddressOf OnOpenClick)End SubPrivateSub OnOpenClick(ByVal sender AsObject, ByVal e As EventArgs)
Dim f AsNew OpenFileDialog
Using f
f.Filter ="txt|*.txt"
f.Multiselect =FalseIf f.ShowDialog = DialogResult.OK ThenDim mRich As RichTextBox =CType(gMainForm.WorkSpace.FindControl(LzmTW.uSystem.uWindows.uForms.MainFormSplitContainer.Panels.Center, "Rich"), RichTextBox)
mRich.LoadFile(IO.File.Open(f.FileName, IO.FileMode.Open), RichTextBoxStreamType.PlainText)
gMainForm.SendMessage(f.FileName)
EndIfEndUsingEnd SubEnd Class
Dim mOpenTxtFileService AsNew OpenTxtFileService
End Sub
現在嘗試做一個通用的主窗體.所謂通用,按我的理解,是儘可能的把常用功能實現在一個類上,應用時僅考慮實際需求.
因為是嘗試,所以這部分程式碼我就不貼上來了,現在只說表現.
主窗體一般的表現有兩種,一是MDI,一是象IDE那種形式.我就將這兩種集合在一起.
使用時,簡單的程式碼就可以實現如下的效果.
程式碼:
PublicClass MainFormInherits LzmTW.uSystem.uWindows.uForms.uMainForm.MainForm
'實現特定選單載入ProtectedOverridesSub MenuManagerRead()
ElseMe.MenuManager.ReadFile()
EndIfEnd SubEnd Class
效果:
切換到MDI模式:
或者用程式碼指定為MDI模式:
PrivateShadowsSub MainForm_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesDim mToolStripMenuItem As ToolStripMenuItem =CType(Me.MenuManager.FindToolStripItem("HideWorkspace"), ToolStripMenuItem)
If mToolStripMenuItem IsNotNothingThen
mToolStripMenuItem.PerformClick()
mToolStripMenuItem.Enabled =FalseEndIfEnd Sub
效果:
做一個簡單的功能實現:
Private gMainForm As LzmTW.uSystem.uWindows.uForms.uMainForm.MainForm
SubNew()
gMainForm =CType(My.Application.ApplicationContext.MainForm, LzmTW.uSystem.uWindows.uForms.uMainForm.MainForm)
Initialize()
End SubPrivateSub Initialize()
With gMainForm.WorkSpace.AddControlTo(LzmTW.uSystem.uWindows.uForms.MainFormSplitContainer.Panels.Center, New RichTextBox)
.Name ="Rich"
.Dock = DockStyle.Fill
EndWith'繫結主窗體[開啟]選單項單擊事件 gMainForm.MenuManager.AddActionHandler("Open", "Click", "OnOpenClick", Me)
'單擊事件也可以這樣繫結'gMainForm.MenuManager.AddClickHandler("Open", AddressOf OnOpenClick)End SubPrivateSub OnOpenClick(ByVal sender AsObject, ByVal e As EventArgs)
Dim f AsNew OpenFileDialog
Using f
f.Filter ="txt|*.txt"
f.Multiselect =FalseIf f.ShowDialog = DialogResult.OK ThenDim mRich As RichTextBox =CType(gMainForm.WorkSpace.FindControl(LzmTW.uSystem.uWindows.uForms.MainFormSplitContainer.Panels.Center, "Rich"), RichTextBox)
mRich.LoadFile(IO.File.Open(f.FileName, IO.FileMode.Open), RichTextBoxStreamType.PlainText)
gMainForm.SendMessage(f.FileName)
EndIfEndUsingEnd SubEnd Class
主窗體Load程式碼改為:
PrivateShadowsSub MainForm_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.LoadDim mOpenTxtFileService AsNew OpenTxtFileService
End Sub
效果:
主窗體本身已含外掛功能,所以可以通過外掛實現功能.