Name Data Type Description

Index Variant The position within the menu bar where the pop-up menu will be added.

It can be either an integer between 0 and N, where N is the number of objects in the menu bar, or a string giving the name of an existing menu (including the & accelerator key character) that the new item will be placed directly before. If the menu specified by the index doesn't exist, then the new menu is added at the end of the menu bar.

This example places a user-named menu at the beginning of the menu bar:

Public Sub InsertMenu1() Dim objMenuGroup As AcadMenuGroup Dim strMenuGroupNames As String Dim strChosenMenuGroup As String Dim objMenu As AcadPopupMenu Dim strMenuNames As String Dim strChosenMenu As String strMenuGroupNames = "Choose one of the loaded menu groups: " & vbCrLf For Each objMenuGroup In ThisDrawing.Application.MenuGroups strMenuGroupNames = strMenuGroupNames & objMenuGroup.Name & vbCrLf Next objMenuGroup strChosenMenuGroup = InputBox(strMenuGroupNames, "Choose a menu group") If strChosenMenuGroup = "" Then Exit Sub

On Error Resume Next

Set objMenuGroup = ThisDrawing.Application.MenuGroups.Item(strChosenMenuGroup) If objMenuGroup Is Nothing Then

MsgBox "The menu group you chose does not exist" Exit Sub End If strMenuNames = "Choose a menu to add to the menu bar:" & vbCrLf For Each objMenu In objMenuGroup.Menus strMenuNames = strMenuNames & objMenu.Name & vbCrLf Next objMenu strChosenMenu = InputBox(strMenuNames, "Choose a menu") If strChosenMenu = "" Then Exit Sub

' check if menu is already on menu bar

Set objMenu = ThisDrawing.Application.MenuBar.Item(strChosenMenu) If Not objMenu Is Nothing Then

MsgBox "This menu is already present on the menu bar" Exit Sub End If

Set objMenu = objMenuGroup.Menus.Item(strChosenMenu) If objMenu Is Nothing Then

MsgBox "The menu you chose does not exist" Exit Sub End If objMenu.InsertInMenuBar 0 End Sub

This example places a user-named menu just before an existing menu, also chosen by the user:

Public Sub InsertMenu2() Dim objMenuGroup As AcadMenuGroup Dim strMenuGroupNames As String Dim strChosenMenuGroup As String Dim objMenu As AcadPopupMenu Dim strMenuNames As String Dim strChosenMenu As String Dim strMenuPosition As String strMenuGroupNames = "Choose one of the loaded menu groups: " & vbCrLf For Each objMenuGroup In ThisDrawing.Application.MenuGroups strMenuGroupNames = strMenuGroupNames & objMenuGroup.Name & vbCrLf

Next strChosenMenuGroup = InputBox(strMenuGroupNames, "Choose a menu group") If strChosenMenuGroup = "" Then Exit Sub

On Error Resume Next

Set objMenuGroup = ThisDrawing.Application.MenuGroups.Item(strChosenMenuGroup) If objMenuGroup Is Nothing Then

MsgBox "The menu group you chose does not exist" Exit Sub End If strMenuNames = "Choose a menu to add to the menu bar:" & vbCrLf For Each objMenu In objMenuGroup.Menus strMenuNames = strMenuNames & objMenu.Name & vbCrLf

Next strChosenMenu = InputBox(strMenuNames, "Choose a menu") If strChosenMenu = "" Then Exit Sub

Set objMenu = objMenuGroup.Menus.Item(strChosenMenu) If objMenu Is Nothing Then

MsgBox "The menu you chose does not exist" Exit Sub End If strMenuPosition = InputBox("Enter name of menu you wish to place it" & _

" before (including accelerator keys)")

objMenu.InsertInMenuBar strMenuPosition

End Sub

Figure 16-5 shows the result of adding the Context Menu for Viewport Object menu of the Viewport menu group before the Draw menu of the ACAD menu group. Note that if the existing menu on the menu bar wasn't recognized, then the new menu will be placed at the end of the menu bar.

Figure 16-5. Setting menu indices

AutoCAD 2006 does this automatically, defaulting to placing all menus on the end. If this is not the desired location, you need to remove them and place them in the right place. AutoCAD 2005 and earlier do not automatically insert the menus.

The second way to add a menu to the menu bar employs the InsertMenuInMenuBar method of a PopupMenus collection.

PopupMenusCollection.InsertMenuInMenuBar MenuName, Index

Table 16-6 shows the InsertMenuInMenuBar method's parameters.

Table 16-6. The InsertMenuInMenuBar Method's Parameters

Was this article helpful?

0 0

Post a comment