当前位置: 开源爱好者 » 编程教程 » VB教程 » 正文

简单的动态菜单制作实例-基于VB代码

VB比较简单的动态菜单制作实例,在窗体中显示出不少于5个的动态菜单组,并且带有分隔条,这种动态的菜单在平时的使用中几乎天天见到,比较基础,也比较实用啦!

VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "Comdlg32.ocx"
Begin VB.Form Form1 
   Caption         =   "动态菜单"
   ClientHeight    =   2100
   ClientLeft      =   165
   ClientTop       =   855
   ClientWidth     =   4800
   BeginProperty Font 
      Name            =   "宋体"
      Size            =   12
      Charset         =   134
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Form1"
   ScaleHeight     =   2100
   ScaleWidth      =   4800
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text1 
      Height          =   2055
      Left            =   0
      TabIndex        =   0
      Text            =   "Text1"
      Top             =   0
      Width           =   4695
   End
   Begin MSComDlg.CommonDialog CommonDialog1 
      Left            =   4080
      Top             =   840
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.Menu file 
      Caption         =   "文件"
      Begin VB.Menu open 
         Caption         =   "打开"
      End
      Begin VB.Menu save 
         Caption         =   "保存"
      End
      Begin VB.Menu a 
         Caption         =   "-"
         Visible         =   0   'False
      End
      Begin VB.Menu menuadd 
         Caption         =   ""
         Index           =   0
         Visible         =   0   'False
      End
      Begin VB.Menu b 
         Caption         =   "-"
      End
      Begin VB.Menu exit 
         Caption         =   "退出"
      End
   End
   Begin VB.Menu edit 
      Caption         =   "编辑"
   End
   Begin VB.Menu menudel 
      Caption         =   "删除菜单项"
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim i
Private Sub open_Click()
CommonDialog1.CancelError = True  '设置纠错语句
  On Error GoTo Err1
  Text1 = ""
  CommonDialog1.Filter = "All Files(*.*)|*.*|文本文件(*.txt)|*.txt|图元文件(*.wmf)|*.wmf"
  CommonDialog1.FilterIndex = 2
  CommonDialog1.ShowOpen   '弹出“打开对话框”
  f = CommonDialog1.FileName
  Open f For Input As #1    '打开文件进行读操作
  Do While Not EOF(1)
    Line Input #1, x
    Text1 = Text1 + x + vbCrLf  '在文本框中显示
  Loop
  Close #1                                '关闭文件
  i = i + 1        '记录文件打开的数量
  If i < 6 Then     '已打开文件的数量不超过5
    a.Visible = True   '显示分隔条
    Load menuadd(i)   '装入新菜单项并显示文件名
    menuadd(i).Caption = CommonDialog1.FileName
    menuadd(i).Visible = True
  Else
   t = i Mod 6
   If t = 0 Then t = 6
   menuadd(t).Caption = CommonDialog1.FileName '刷新标题
   End If
  Exit Sub
Err1:       '错误处理
  If Err.Number = 32755 Then Exit Sub  '单击“取消”按钮
End Sub

Private Sub save_Click()
CommonDialog1.ShowSave                    '弹出“保存”对话框
Open CommonDialog1.FileName For Output As 1  '打开文件
Print #1, Text1    '写操作
Close 1
End Sub

Private Sub menudel_Click()
If i > 0 Then
   Unload menuadd(i) '删除菜单
   i = i - 1
End If
If i = 0 Then a.Visible = False  '菜单项被删完,隐藏分隔条
End Sub
Private Sub exit_Click()
End  '结束
End Sub

CopyRight 2018© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图