|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97
Chapter 7 AddInMaker
Like many programming tasks, creating add-ins is simple taken one step at a time. There are enough interrelated steps, however, that making a mistake is easy. For instance, if the steps taken in ConnectAddIn are not correctly reversed in DisconnectAddIn, the add-in will not properly uninstall itself. This would leave useless menu items in the Add-Ins menu after the add-in server has stopped.
Add-ins were invented to automate routine programming tasks. Although building add-ins may not exactly be routine, it is certainly possible to automate the process. AddInMaker is an add-in that helps create other add-ins.
The first section in this chapter, Using AddInMaker, describes the add-in from the developers point-of-view. It explains how the developer can use the add-in to create other add-ins. The Key Techniques section briefly lists the main programming techniques used to implement AddInMaker. The rest of the chapter describes these techniques in detail.
Using AddInMaker
To begin creating an add-in, the developer selects the Create Add-In command from the Add-Ins menu. AddInMaker displays a dialog similar to the one shown in Figure 7.1
In this dialog, the developer defines menu entries for the new add-in. The Project Name field indicates the name of the server project as it will be used by the Add-In Manager. For example, if a server class object will be referenced by MyProject.MyClass, then the project name is MyClass.
The developer enters the names of the submenus and commands that should be placed in the Add-Ins menu, checking the Submenu checkbox for the submenus. For menu commands, the developer enters the name of the class that will handle that command.
For example, the entries shown in Figure 7.1 define one submenu containing two menu commands. The commands will be handled by the AfterClick subroutines in the Test-AddIn.RunTest and TestAddIn.AboutTest classes. Figure 7.2 shows the menus produced by these entries. You can find the source code for this test add-in in the Ch7\TestAdd directory on the compact disk.
FIGURE 7.1 The AddInMaker dialog.
When the developer clicks the OK button, AddInMaker writes a Main subroutine that registers the new add-in in VB.INI. To make installing the add-in in Visual Basic 5 easier, AddInMaker also creates an AddToINI subroutine that registers the add-in in VBADDIN.INI rather than VB.INI. To create the initialization file entries in Visual Basic 5, the developer can execute AddToINI in Visual Basics Immediate window. In Visual Basic 4, AddToINI would be unused.
AddInMaker also creates a class called Connector that provides the ConnectAddIn and Disconnect-AddIn subroutines that install and uninstall the add-ins menu items. Finally, AddInMaker builds classes providing AfterClick events to respond to the menu commands.
FIGURE 7.2 The Add-Ins menu commands created by the entries shown in Figure 7.1.
To finish creating the add-in, the developer must add code to the AfterClick event handlers and perform the normal add-in installation tasks. These tasks include setting the project name, setting StartMode to OLE Server, and creating the OLE DLL file. When it finishes, AddInMaker presents a dialog reminding the developer to perform these tasks.
Key Techniques
Architecturally, AddInMaker is similar to the PropertySetter and Aligner add-ins described in previous chapters. All three of these add-ins present a dialog to obtain information from the user. All three provide a Help button so all three need only a single command in the Add-Ins menu. This makes AddInMakers ConnectAddIn and DisconnectAddIn subroutines similar to those used by PropertySetter and Aligner (they are not reproduced here).
In a way, AddInMaker is an in-depth review of how add-ins work. To correctly write an add-in that creates other add-ins, one must understand add-in concepts fairly well.
The following list briefly describes the key techniques used to implement AddInMaker. The rest of this chapter describes these techniques in detail.
- Managing AddInMakers Dialog. AddInMakers AfterClick subroutine presents the add-ins dialog. This section shows how AddInMaker uses the dialog to gather information from the user. The following sections describe the routines that use the information to create the new add-in.
- Building Sub Main. The Main and AddToINI subroutines register the new add-in with the appropriate initialization files. This section explains how AddInMaker creates these routines for the new add-in.
- Building ConnectAddIn and DisconnectAddIn. Subroutine ConnectAddIn creates the submenus and menu commands displayed for the new add-in in the Add-Ins menu. Subroutine DisconnectAddIn removes the entries created by ConnectAddIn. This section tells how AddInMaker creates these two important subroutines.
- Creating Command Classes. For each menu command specified by the developer, AddInMaker creates a class with an AfterClick event handler. This section shows how AddInMaker creates these classes.
Managing AddInMakers Dialog
AddInMakers AfterClick event subroutine presents the dialog. If the user clicks the OK button, AfterClick invokes the CreateAddIn subroutine. CreateAddIn calls other subroutines to create the new add-ins Main and AddToINI subroutines, the Connector class that contains the ConnectAddIn and DisconnectAddIn routines, and the menu command classes themselves.
When it is finished, CreateAddIn presents AddInMakers AboutDialog. AboutDialog is used both to present simple help when the user clicks the dialogs Help button and as a reminder to perform extra add-in installation chores after the add-in has been built. By setting AboutDialogs About property to false, CreateAddIn tells it to format itself as a reminder dialog rather than as a help screen.
Sub CreateAddIn()
Create Sub Main.
CreateSubMain
Create the connector class.
CreateConnector
Create the classes.
CreateClasses
AboutDialog.About = False
AboutDialog.Show vbModal
End Sub
|