3 4
Using ActiveX controls in your Microsoft Visio solution allows you to create a user interface that is consistent with solutions based on other Microsoft Windows-based applications. Because the controls are on the drawing page, the user can work freely with both controls and Visio shapes in a drawing.
To work with ActiveX controls in a Visio drawing, switch between design mode and run mode. In design mode, you can insert controls, move and size them, and set their properties. In run mode, you can use the controls—click a command button to run its Click event handler, for example. For other tasks, it doesn't matter whether Microsoft Visio is in design mode or run mode—all other Visio commands and tools work the same way in either mode.
The document's mode is synchronized with that of its Microsoft Visual Basic for Applications (VBA) project, so both the document and its project are always in the same mode. While a document is in design mode, none of its objects (including controls) issues events.
A Visio document opens in run mode by default, unless the macros in the document have been disabled. Macros are enabled or disabled depending on the security level setting for Visio and whether the VBA project has been digitally signed.
To switch to design mode, make sure the Developer toolbar is displayed. If the toolbar is not displayed, on the View menu, point to Toolbars, click Developer, and then click the Design Mode button on the Developer toolbar. The button inverts to indicate that Visio is in design mode.
Note
To disable macros programmatically when a document opens, use the visOpenMacrosDisabled flag in the OpenEx method of a Documents collection.
Visio also provides policy support for security, which allows an administrator to limit the security level that a user can set. For more details, see Using Digital Certificates to Produce Trusted Solutions in Chapter 15, Programming Visio with VBA.
Before you can insert an ActiveX control in a Visio drawing, the control must be installed on your system. Certain controls can also require that you have a design license to use them in applications that you develop.
You insert a control by selecting it in the Control dialog box, which lists all of the ActiveX controls installed on your system, including those installed by other applications. Such applications typically provide a run-time license for the ActiveX controls they contain. The run-time license entitles you to use those controls in the application that contains them but not to insert the controls in applications that you develop. To insert such controls in your applications, you need a design-time license. For details, see Distributing ActiveX Controls in a Visio Solution later in this chapter.
Figure 24-1 Control dialog box.
A selected control has green selection handles, just like a selected shape, and you move and size the control the same way.
Figure 24-2 A selected control.
To edit a control, double-click it. A control activated for in-place editing in this way looks the same in Microsoft Visio as in any ActiveX container. To set a control's properties or write code for the control, right-click the control and point to the name of the control object on the shortcut menu. You can then select a command that allows you to modify the control using the Visual Basic Editor.
Figure 24-3 A control activated for in-place editing.
After you insert a control in a drawing, you can work with it in the same way as with a Visio shape. For example, you can cut or copy and paste the control, duplicate it with CTRL+drag, or make it into a master by dragging it to a stencil.
The first time you insert a control into a Visio drawing, VBA creates a "merged type library" for the control and stores it in your Temp folder in a file with an .exd file name extension. If you subsequently modify the control, delete the EXD file before attempting to insert the control again. If you receive an error message when you insert a control after modifying it, you can delete the cached EXD file to correct the problem.
If your control supports the IClassFactory2 interface, Visio uses that interface when attempting to insert a new instance of that control. Therefore, the insertion succeeds only on systems in which the control is properly licensed. If your control does not support IClassFactory2, Visio uses the IClassFactory interface to instantiate the control, and licensing is not a consideration.
When Microsoft Visio is in run mode, pressing the TAB key moves the focus from one control to another on the drawing page. If you add more than one control to a drawing, you'll want the focus to move in a logical order.
The tabbing order of controls corresponds to the stacking order of the controls on the drawing page, starting with the backmost control. Initially, this is the order in which you inserted the controls in the drawing, with the most recently inserted control at the front.
To change the stacking order of controls relative to each other, use the following commands from the Order submenu on the Shape menu:
If you're developing ActiveX controls for use in Microsoft Visio, you can take advantage of the ambient properties that Visio defines. A control uses an application's ambient properties to maintain a consistent appearance with other controls in a document. For example, the BackColor property specifies the color of a control's interior.
Ambient properties are read-only.
If you want the user to be able to print a drawing but not its controls, do one of the following:
For details about layers, see Chapter 11, Arranging Shapes in Drawings.
When you distribute a solution that contains controls, you might want users to have the ability to edit the shapes in the drawing but not the controls. You can protect controls from user changes, even in design mode, by locking the shapes and protecting the document.
The user will be able to modify the drawing but not the controls.