- 积分
- 10513
- 明经币
- 个
- 注册时间
- 2002-6-3
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2014-1-14 13:16:56
|
显示全部楼层
创建三个通用函数,简化操作。
- ' AddMenu 在菜单组中创建菜单,如果菜单已经存在,直接返回
- ' mg 菜单组
- ' name 要创建的菜单名称
- Function AddMenu(ByVal mg As AcadMenuGroup, ByVal name As String) As AcadPopupMenu
- Dim pm As AcadPopupMenu
- Dim b As Boolean
- b = False
- Dim i As Integer
- For i = 0 To mg.Menus.Count - 1
- If mg.Menus(i).name = name Then
- Set pm = mg.Menus(i)
- b = True
- Exit For
- End If
- Next
- If b = False Then
- Set pm = mg.Menus.Add(name)
- End If
- Set AddMenu = pm
- End Function
- ' AddSubMenu 在菜单、子菜单中创建子菜单,如果子菜单已经存在,直接返回
- ' parentpm 父级菜单
- ' caption 要创建的子菜单标题
- Function AddSubMenu(ByVal parentpm As AcadPopupMenu, ByVal caption As String) As AcadPopupMenu
- Dim pm As AcadPopupMenu
- Dim b As Boolean
- b = False
- Dim i As Integer
- For i = 0 To parentpm.Count - 1
- If parentpm(i).caption = caption Then
- Set pm = parentpm(i).SubMenu
- b = True
- Exit For
- End If
- Next
- If b = False Then
- Set pm = parentpm.AddSubMenu(parentpm.Count + 1, caption)
- End If
- Set AddSubMenu = pm
- End Function
- ' AddMenuItem 在菜单、子菜单中创建菜单项,如果delete为真,先删除菜单项再创建。如果delete为假,菜单项已经存在时直接返回
- ' parentpm 父级菜单
- ' label 要创建的菜单项标签
- ' macro 要创建的菜单项宏
- ' delete 是否先删除菜单项
- Function AddMenuItem(ByVal parentpm As AcadPopupMenu, ByVal label As String, ByVal macro As String, Optional ByVal delete As Boolean = True) As AcadPopupMenuItem
- Dim pmi As AcadPopupMenuItem
- Dim b As Boolean
- b = False
- Dim i As Integer
- For i = 0 To parentpm.Count - 1
- If parentpm(i).caption = label Then
- Set pmi = parentpm(i)
- If delete = True Then
- pmi.delete
- b = False
- Else
- b = True
- End If
- Exit For
- End If
- Next
- If b = False Then
- Set pmi = parentpm.AddMenuItem(parentpm.Count + 1, label, macro)
- End If
- Set AddMenuItem = pmi
- End Function
最后过程:
- Function 创建三级菜单按钮(一级菜单名, 二级菜单名, 三级菜单名, VBA或LSP, 按钮标题, 命令)
- 'On Error Resume Next
- Dim macro As String
- macro = Chr(vbKeyEscape) + Chr(vbKeyEscape) '这是在向命令行发送命令时需要的前缀。
- Dim currMenuGroup As AcadMenuGroup
- Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item(0)
- '下面是创建菜单“工”
- Dim 菜单一 As AcadPopupMenu
- Set 菜单一 = AddMenu(currMenuGroup, 一级菜单名 & Chr(Asc("&"))) '这是说在没有时创建菜单“小多”
-
- Dim 菜单二 As AcadPopupMenu
- Set 菜单二 = AddSubMenu(菜单一, 二级菜单名 & Chr(Asc("&")))
-
- Dim 菜单三 As AcadPopupMenu
- Set 菜单三 = AddSubMenu(菜单二, 三级菜单名 & Chr(Asc("&")))
-
- Dim subMenuItemPoint As AcadPopupMenuItem
- If VBA或LSP = "LSP" Then
- '创建程序文件简单名为名的按钮,单击按钮时发出"-VBARUN XIANSHISHIYIANDAIMAYONGCHUANGTI "命令
- Set subMenuItemPoint = AddMenuItem(菜单三, Chr(Asc("&")) & 按钮标题, macro & 命令 & " ")
- Else
- Set subMenuItemPoint = AddMenuItem(菜单三, Chr(Asc("&")) & 按钮标题, macro & "-VBARUN " & 命令 & " ")
- End If
-
- Dim b As Boolean
- b = False
- Dim i As Integer
- For i = 0 To ThisDrawing.Application.MenuBar.Count - 1
- If ThisDrawing.Application.MenuBar(i).name = 一级菜单名 & Chr(Asc("&")) Then
- b = True
- Exit For
- End If
- Next
- If b = False Then
- 菜单一.InsertInMenuBar (ThisDrawing.Application.MenuBar.Count + 1)
- End If
这样整个过程就清晰了,对于四级、五级菜单,只要重复调用AddSubMenu函数即可。 |
|