Visio2000: How to Bind a Custom Accelerator Key for a Custom Menu to a Specific Drawing Window (281504)
The information in this article applies to:
- Microsoft Visio 2000 Standard Edition Service Release 1 (SR-1)
- Microsoft Visio 2000 Professional Edition Service Release 1 (SR-1)
- Microsoft Visio 2000 Technical Edition Service Release 1 (SR-1)
- Microsoft Visio 2000 Enterprise Edition Service Release 1 (SR-1)
This article was previously published under Q281504 SUMMARY
This article demonstrates how to:
- Create a custom menu and accelerator.
- Turn off the registered built-in accelerator and replace it with a custom accelerator "on demand" for a window.
- Save the custom accelerator to a custom Visio user interface file (.vsu).
- Turn off the registered built-in accelerator and replace it with a custom accelerator when opening a drawing file.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without warranty either
expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes
that you are familiar with the programming language being demonstrated and the
tools used to create and debug procedures. Microsoft support professionals can
help explain the functionality of a particular procedure, but they will not
modify these examples to provide added functionality or construct procedures to
meet your specific needs. If you have limited programming experience, you may
want to contact a Microsoft Certified Partner or the Microsoft fee-based
consulting line at (800) 936-5200. For more information about Microsoft Certified
Partners, please visit the following Microsoft Web site:
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
Custom Menu and Accelerator Example
Visio 2000 contains two UIObject objects: a built-in menu and a custom menu, each of which is associated with a specific view window in Visio. Each type of view window in Visio (Drawing, ShapeSheet, or Stencil) has a specific set of menu options available based on which window currently has the active window with focus. The active window in Visio determines the context that is applied to the menu objects (see SetID property for further information).
Applying custom menu and accelerator settings to a window context using the ActiveDocument CustomMenusFile method adds only the custom menu and accelerator. This action does not modify or "mask out" the already registered accelerator from the accelerators list associated to the built-in menus. Built-in accelerators have a higher priority, and therefore a custom menu or accelerator item with the same ID as the built-in menu for that window may never receive the event messaging due to the built-in UIObject receiving and handling the event message. For each window context or "view" of the drawing, the accelerator key must be removed (that is, masked off) explicitly from the built-in. This removal is best performed in the DocumentOpened procedure.
The following example creates a custom menu and custom menu item to execute an add-on called ShowArgs.exe. The accelerator key for the menu item is set to CTRL+O. Because CTRL+O is the default keyboard shortcut to display the Open dialog box, the example deletes the accelerator key for menu item Open on the File menu.
Showargs.exe is in the Visio\DVS\VB Solutions\ShowArgs folder on the Visio product CD. It displays the arguments passed to the application. To ensure this sample works correctly, follow these steps:
- Copy Showargs.exe to the Program Files\Visio\Solutions folder.
- In Visio, point to Macros on the Tools menu, and check to see that Showargs is in the drop-down list.
To create a custom menu, follow these steps:
- Open a new blank drawing in Visio 2000 (press CTRL+N).
- Press ALT+F11 to open the Visual Basic Editor. On the Insert menu, click Module.
- Insert the following code in the module:
' Creates Customer menu and accelerator item for Ctrl+O keys.
Public Sub AddMenuItem_Example()
Dim UIObj As Visio.UIObject
Dim menuSetsObj As Visio.MenuSets
Dim menuSetObj As Visio.MenuSet
Dim menusObj As Visio.Menus
Dim menuObj As Visio.Menu
Dim menuItemsObj As Visio.MenuItems
Dim menuItemObj As Visio.MenuItem
Dim accelTblsObj As Visio.AccelTables
Dim accelTblObj As Visio.AccelTable
Dim accelItemsObj As Visio.AccelItems
Dim accelItemObj As Visio.AccelItem
'Get a UI object that represents Visio's built-in menus
Set UIObj = Visio.Application.BuiltInMenus
'Get the MenuSets collection
Set menuSetsObj = UIObj.MenuSets
Set accelTblsObj = UIObj.AccelTables
'Get the drawing window menu and accelerator sets
Set menuSetObj = menuSetsObj.ItemAtID(visUIObjSetDrawing)
Set accelTblObj = accelTblsObj.ItemAtID(visUIObjSetDrawing)
'Get the menus & accelerators collection
Set menusObj = menuSetObj.Menus
Set accelItemsObj = accelTblObj.AccelItems
'Add a Demo menu before the Window menu.
Set menuObj = menusObj.AddAt(7)
menuObj.Caption = "&Demo"
menuObj.Enabled = True
'Get the menuItems collection
Set menuItemsObj = menuObj.MenuItems
'Find the accelerator object to remove for the File command open
For i = 0 To accelItemsObj.Count - 1
Set accelItemObj = accelItemsObj.Item(i)
If accelItemObj.CmdNum = Visio.visCmdFileOpen Then
Exit For
End If
Next i
'Delete the accelerator for File Open (Ctrl + O)
accelItemObj.Delete
'Add a menu item and accelerator to the new Demo menu
Set menuItemObj = menuItemsObj.Add
Set accelItemObj = accelItemsObj.Add
'Set the properties for the new menu item
With menuItemObj
.Caption = "Run ShowArgs Ctrl+O"
.AddOnName = "ShowArgs.EXE"
.AddOnArgs = "/DVS=Fun"
.ActionText = "Run ShowArgs"
.MiniHelp = "Run the ShowArgs application"
.Enabled = True
End With
'Set the properties of the new accelerator
'Must set the Key = to the correct Virtual Key
'code VK_O == 79
With accelItemObj
.Control = True
.Key = 79
.AddOnName = "ShowArgs.EXE"
.AddOnArgs = "/DVS=Fun"
End With
'Save the UI changes to disk
'Change this line to save the UI to location other then c:\
UIObj.SaveToFile "c:\Custom_Sample.vsu"
'Tell Visio to use the new UI when the document is active
ThisDocument.SetCustomMenus UIObj
End Sub
- Return to the Visio application window, and ensure the new blank drawing has focus.
- Press ALT+F8 to open the Run Macros dialog box, and then set the Macros in drop-down to Module1.
- Select AddMenuItem_Example, and then click the Run button.
NOTE: The "Demo" menu item is added before the "Window" menu. At this point you should be able to press CTRL+O and see that the ShowArgs application dialog box is opened rather than the Open dialog box.
Loading the Custom Menu and Accelerator When Opening Drawing- In the Visual Basic Editor, select Module1 with code (from steps noted above). On the File menu, click "Remove Module1..." from the Projects window.
- Double-click ThisDocument (Drawing1) under the Visio Objects folder to open a new code window for the ThisDocument.
- In the Visual Basic Editor project window, add the following code to explicitly remove the accelerator, and load the custom menu and accelerator items:
' Example removes the accelerator keys for the Open dialog box (CTRL+0)
' and loads the custom menu file (.VSU) with a the same accelerator keys
' defined for a Visual Basic macro to execute instead for this drawing ' window only.
'
Private Sub Document_DocumentOpened(ByVal doc As IVDocument)<BR/>
Dim UIObj As Visio.UIObject
Dim accelTblsObj As Visio.AccelTables
Dim accelTblObj As Visio.AccelTable
Dim accelItemsObj As Visio.AccelItems
Set UIObj = Visio.Application.BuiltInMenus
Set accelTblsObj = UIObj.AccelTables
Set accelTblObj = accelTblsObj.ItemAtID(visUIObjSetDrawing)
Set accelItemsObj = accelTblObj.AccelItems
'Find the accelerator object to remove for the File command open
For i = 0 To accelItemsObj.Count - 1
Set accelItemObj = accelItemsObj.Item(i)
If accelItemObj.CmdNum = Visio.visCmdFileOpen Then
Exit For
End If
Next i
'Delete the accelerator for File Open (Ctrl + O)
accelItemObj.Delete
'Change this line it the UI in a location other then c:\
ThisDocument.CustomMenusFile = "c:\Custom_Sample.vsu"
End Sub
- Switch back to the Visio application window. On the File menu, click Save As.
- Save this one Visio drawing under three different file names, such as Drawing1.vsd, Drawing2.vsd, and Drawing3.vsd.
- Quit Visio and restart Visio with a new blank drawing (CTRL+N).
NOTE: No custom menu item appears in the menu. When you press CTRL+O, the Open dialog box opens. - Open each of the three drawings that you just saved.
- Switch back and forth between the new blank drawing and the three saved drawings. Test the accelerator keys and notice the accelerator is specific to a drawing document window within Visio.
REFERENCES
The sample code to remove the built-in accelerator and add the custom menu and accelerator item is based on samples in Chapter 22, "Customizing the Visio User Interface", found in Developing Visio Solutions for Microsoft Visio 2000.
Various topics can be found by querying on "Virtual Key codes" on the MSDN website at http://www.msdn.microsoft.com
Modification Type: | Minor | Last Reviewed: | 1/6/2006 |
---|
Keywords: | kbhowto KB281504 |
---|
|