1、MDI窗体程序代码Option ExplicitPublic intChildNum As Integer 字窗体数 变量Public intCaptionNum As Integer 变换标题名称 变量Public blnSaveAA As Boolean 与 strCaptionAA 确定“另存为“ 操作完后不出现消息框可直接退出Public strCaptionAA As String 用于确定“ 另存为“操作完后只有按本窗口关闭时有用并不出现消息框Public blnSaveBB As Boolean 与 strCaptionBB 确定“ 保存“操作完后不出现消息框可直接退出Publi
2、c strCaptionBB As String 用于确定“另存为“ 操作完后只有按本窗口关闭时有用并不出现消息框Private Sub MDIForm_Load()mnuWindow.Visible = False 开始时不显示“窗体“菜单mnuFileSave.Visible = False 开始时不显示“ 保存 “菜单mnuFileSaveAnother.Visible = False 开始时不显示“另存为“菜单mnuEditor.Visible = False 开始时不显示“ 编辑 “菜单mnuO.Visible = False 开始时不显示 “格式“菜单End SubPrivate
3、Sub mnuEditor_Click()If ActiveForm.ActiveControl.SelText = “ ThenmnuEditorEdi(0).Enabled = FalsemnuEditorEdi(1).Enabled = FalsemnuEditorEdi(3).Enabled = FalseIf Clipboard.GetText = “ ThenmnuEditorEdi(2).Enabled = FalseElseIf Clipboard.GetText “ ThenmnuEditorEdi(0).Enabled = TruemnuEditorEdi(1).Enabl
4、ed = TruemnuEditorEdi(3).Enabled = TrueIf Clipboard.GetText = “ ThenmnuEditorEdi(2).Enabled = FalseElseIf Clipboard.GetText “ ThenmnuEditorEdi(2).Enabled = TrueEnd IfEnd IfEnd SubPrivate Sub mnuEditorEdi_Click(Index As Integer)Select Case IndexCase 0Clipboard.SetText ActiveForm.ActiveControl.SelText
5、 剪切ActiveForm.ActiveControl.SelText = “Case 1Clipboard.SetText ActiveForm.ActiveControl.SelText 复制Case 2ActiveForm.ActiveControl.SelText = Clipboard.GetText 粘贴Case 3ActiveForm.ActiveControl.SelText = “ 删除End SelectEnd SubPrivate Sub mnuEditortTD_Click()Dim str1 As StringDim str2 As Stringstr1 = Mid(
6、ActiveForm.ActiveControl.Text, 1, Len(ActiveForm.ActiveControl.Text) 原文本内容str2 = Now 时间及日期ActiveForm.ActiveControl.Text = str1 + str2 原文本内容 + 时间及日期End SubPrivate Sub mnuFileSave_Click()Dim strTxt As StringstrTxt = ActiveForm.Txt1.TextCommonDialog1.Filter = “文本文档 (*.txt)|*.txt“ “保存“ 公共对话框只显示文本文件Commo
7、nDialog1.ShowSave 弹出公共对话框If CommonDialog1.FileName = “ Then Exit Sub 若按取消键则公共对话框消失Open CommonDialog1.FileName For Output As #1Print #1, strTxt 写文件Close #1blnSaveBB = TruestrCaptionBB = ActiveForm.CaptionCall subAprcFrmEnd SubPrivate Sub mnuOFont_Click()CommonDialog1.Flags = cdlCFBoth Or cdlCFEffects
8、CommonDialog1.FontName = “宋体“CommonDialog1.ShowFont 打开字体对话框ActiveForm.Txt1.FontName = CommonDialog1.FontName 改变文本字体格式ActiveForm.Txt1.FontSize = CommonDialog1.FontSizeActiveForm.Txt1.FontBold = CommonDialog1.FontBoldActiveForm.Txt1.FontItalic = CommonDialog1.FontItalicActiveForm.Txt1.FontStrikethru =
9、 CommonDialog1.FontStrikethruActiveForm.Txt1.FontUnderline = CommonDialog1.FontUnderlineActiveForm.Txt1.ForeColor = CommonDialog1.ColorEnd SubPrivate Sub mnuFileExit_Click()Unload Me 窗体卸载End SubPrivate Sub mnufilenew_Click()Static i As Integer 设置静态变量作为标题属性取值If i 1 Then i = 1 设置初值i = i + intCaptionNu
10、mDim newform As New Form1newform.Caption = “文档“ + Str(i)i = i + 1 标题数目增加intCaptionNum = 0 模块级变量值重新为 0intChildNum = intChildNum + 1 子窗体数加 1mnuWindow.Visible = True 显示“窗体“菜单mnuFileSave.Visible = True 显示“保存“菜单mnuFileSaveAnother.Visible = True 显示“另存为“菜单mnuEditor.Visible = True 显示“编辑“菜单mnuO.Visible = Tru
11、e 显示“格式“菜单newform.Show 显示子窗体End SubPrivate Sub mnuFileOpen_Click()Dim str1 As StringDim str2 As StringCommonDialog1.Filter = “文本文件 (*.txt)|*.txt“ 打开公共对话框只显示文本文件CommonDialog1.ShowOpen 打开公共对话框If CommonDialog1.FileName = “ Then Exit Sub 若按取消键则公共对话框消失Open CommonDialog1.FileName For Input As #2Do While N
12、ot EOF(2) 函数 EOF() 用于判断是否到文件 2 末尾Line Input #2, str1 读文件str2 = str2 + str1 + vbCrLfLoopClose #2mnuWindow.Visible = True 显示“窗体“菜单mnuFileSave.Visible = True 显示“保存“菜单mnuFileSaveAnother.Visible = True 显示“另存为“菜单mnuEditor.Visible = True 显示“编辑“菜单mnuO.Visible = True 显示“格式“菜单Dim newform1 As New Form1newform1
13、.Caption = CommonDialog1.FileName 窗体标题名intCaptionNum = intCaptionNum + 1 窗体标题名取值加 1intChildNum = intChildNum + 1 子窗体数加 1newform1.Txt1.Text = str2End SubPrivate Sub mnuFileSaveAnother_Click()Dim strTxt As StringstrTxt = ActiveForm.Txt1.TextCommonDialog1.Filter = “文本文档 (*.txt)|*.txt“ “保存“ 公共对话框只显示文本文件
14、CommonDialog1.ShowSave 弹出公共对话框If CommonDialog1.FileName = “ Then Exit Sub 若按取消键则公共对话框消失Open CommonDialog1.FileName For Output As #1Print #1, strTxt 写文件Close #1blnSaveAA = TruestrCaptionAA = ActiveForm.CaptionCall subAprcFrmEnd SubPrivate Sub mnuWindowArrange_Click(Index As Integer)Select Case Index
15、按不同要求排列窗体Case 0MDIForm1.Arrange 0Case 1MDIForm1.Arrange 1Case 2MDIForm1.Arrange 2Case 3MDIForm1.Arrange 3End SelectEnd SubPrivate Sub subAprcFrm()If intChildNum = 0 ThenmnuWindow.Visible = False 无子窗体时不显示“窗体“菜单mnuEditor.Visible = False 无子窗体时不显示“ 编辑“菜单mnuO.Visible = False 无子窗体时不显示 “格式“菜单End IfEnd Sub子
16、窗体程序代码Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)Dim int1 As IntegerDim strTxt As StringstrTxt = Txt1.Text 为写文件作准备If MDIForm1.blnSaveAA And MDIForm1.strCaptionAA = MDIForm1.ActiveForm.Caption ThenMDIForm1.blnSaveAA = FalseMDIForm1.intChildNum = MDIForm1.intChildNum - 1 子窗体
17、数减 1Call subAprcFrmExit Sub 另存为后只有关闭本窗体才不弹出消息框End IfIf MDIForm1.blnSaveBB And MDIForm1.strCaptionBB = MDIForm1.ActiveForm.Caption ThenMDIForm1.blnSaveBB = FalseMDIForm1.intChildNum = MDIForm1.intChildNum - 1 子窗体数减 1Call subAprcFrmExit Sub 另存为后只有关闭本窗体才不弹出消息框End IfIf Len(strTxt) = 0 ThenMDIForm1.intCh
18、ildNum = MDIForm1.intChildNum - 1 子窗体数减 1Call subAprcFrm 调用 subAprcFrm 过程Exit SubEnd Ifint1 = MsgBox(MDIForm1.ActiveForm.Caption + “尚未保存,退出后是否保存? “, 51, “注意!“) 消息框If int1 = vbYes ThenCommonDialog1.Filter = “文本文档(*.txt)|*.txt“ “保存“ 公共对话框只显示文本文件CommonDialog1.ShowSave 弹出公共对话框If CommonDialog1.FileName =
19、 “ Then Cancel = 1: Exit Sub 若按取消键则公共对话框消失Open CommonDialog1.FileName For Output As #1Print #1, strTxt 写文件Close #1MDIForm1.intChildNum = MDIForm1.intChildNum - 1 子窗体数减 1Call subAprcFrmElseIf int1 = vbNo ThenMDIForm1.intChildNum = MDIForm1.intChildNum - 1 子窗体数减 1Call subAprcFrmExit Sub 不保存退出ElseIf in
20、t1 = vbCancel ThenCancel = 1 停止卸载End IfEnd SubPrivate Sub Form_Resize() 当改变窗体大小时文本框大小同时改变Txt1.Top = 0Txt1.Left = 0Txt1.Width = ScaleWidthTxt1.Height = ScaleHeightEnd SubPrivate Sub subAprcFrm()If MDIForm1.intChildNum = 0 ThenMDIForm1.mnuWindow.Visible = False 无子窗体时不显示“窗体“ 菜单MDIForm1.mnuEditor.Visibl
21、e = False 无子窗体时不显示“ 编辑“菜单MDIForm1.mnuO.Visible = False 无子窗体时不显示“格式“ 菜单End IfEnd Sub窗体说明1. 将 Form1 中文本 txt1 的 MultiLine 属性设置为 Ture 。ScrollBar 属性设置为 3-Both。2. 将 Form1 的 MDIChild 属性设置为 True. 当对 Form1 上安放一 OLE 控件,嵌入对象为 Word 时将 NegotiateMenus 属性设置为True1. 在“打开”公共对话框后直接按取消出现问题2. “另存为”完毕后其相应窗体却未卸载掉3. 卸载时消息框提示信息不随文本框标题名变化3. Form1 及 MDIForm1 中 Icon 属性加载图标应找到硬盘中相应图标所在路径4. “工程” “部件”中选择”Microsoft Common Dialog Control 6.0” 然后在 MDIForm 上添加公共对话框空件5. 将菜单”窗口 ”的 WindowList 属性设置为 Ture