Microsoft KB Archive/281504

= Visio2000: How to Bind a Custom Accelerator Key for a Custom Menu to a Specific Drawing Window =

Article ID: 281504

Article Last Modified on 3/1/2007

-

APPLIES TO


 * Microsoft Visio 2000 Service Release 1
 * Microsoft Visio 2000 Service Release 1
 * Microsoft Visio 2000 Service Release 1
 * Microsoft Visio 2000 Service Release 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 &quot;on demand&quot; 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 INFORMATION
Microsoft 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:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

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 &quot;mask out&quot; 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 &quot;view&quot; 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:
 * 1) Copy Showargs.exe to the Program Files\Visio\Solutions folder.
 * 2) 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 = &quot;&Demo&quot; 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 = &quot;Run ShowArgs                   Ctrl+O&quot; .AddOnName = &quot;ShowArgs.EXE&quot; .AddOnArgs = &quot;/DVS=Fun&quot; .ActionText = &quot;Run ShowArgs&quot; .MiniHelp = &quot;Run the ShowArgs application&quot; .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 = &quot;ShowArgs.EXE&quot; .AddOnArgs = &quot;/DVS=Fun&quot; End With 'Save the UI changes to disk 'Change this line to save the UI to location other then c:\ UIObj.SaveToFile &quot;c:\Custom_Sample.vsu&quot; '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 &quot;Demo&quot; menu item is added before the &quot;Window&quot; 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.</ol>

Loading the Custom Menu and Accelerator When Opening Drawing
<ol> In the Visual Basic Editor, select Module1 with code (from steps noted above). On the File menu, click &quot;Remove Module1...&quot; from the Projects window.</li> Double-click ThisDocument (Drawing1) under the Visio Objects folder to open a new code window for the ThisDocument.</li>  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 = &quot;c:\Custom_Sample.vsu&quot; End Sub </li> Switch back to the Visio application window. On the File menu, click Save As.</li> Save this one Visio drawing under three different file names, such as Drawing1.vsd, Drawing2.vsd, and Drawing3.vsd.</li> 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.</li> Open each of the three drawings that you just saved.</li> 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.</li></ol>

<div class="references_section">