![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
AddInMakers most interesting code is contained in the three subroutines CreateSubMain, CreateConnector, and CreateClasses. These subroutines are described in the following sections. Building Sub MainThe CreateSubMain subroutine writes the new add-ins Main and AddToINI subroutines. It creates a file named SubMain.BAS in the development environments default directory. It then writes the code that belongs in the new add-ins main module using Print statements. CreateSubMain begins by placing the following statement in the new file: Attribute VB_Name = SubMain This statement tells the programming environment that the new modules name is SubMain. When the developer loads this file into a project, this name appears in the Name field on the modules Property screen. The main task of subroutines Main and AddToINI is to register the class containing the ConnectAddIn and DisconnectAddIn subroutines in the appropriate initialization file. AddInMaker gives the new add-in a class named Connector that contains these subroutines. CreateSubMain uses the project name entered in the AddInMaker dialog as the new add-ins name. For example, if the developer entered TestAddIn in this field, CreateSubMain writes code to register the application as TestAddIn.Connector in the initialization file. Writing Visual Basic source code into a file can be a little strange. Printing double quotation marks can be particularly tricky. Inside a string the code must use two double quotation marks to indicate that the Print statement should print one double quotation mark. A quotation mark at the end of a string appears even stranger. Two double quotation marks in the string print one double quotation mark in the file. Then a third double quotation mark ends the string. For example, consider the following statement: Print #fp, Attribute VB_Name = SubMain This statement produces the following output in the file: Attribute VB_Name = SubMain When CreateSubMain has finished writing the main modules source code, it closes the file SubMain.BAS. It then uses the active projects AddFile method to add the file to the project. This is different from the InsertFile method used by Scroller to add source code to a form. The InsertFile method copies a file into one of the projects existing modules. AddFile makes the file a new module within the project. The following code shows CreateSubMain. The code that produces the API function declarations has been omitted. This code is similar to code used in previous programs so it is nothing new. It is quite long and formats so badly it is hard to read anyway.
Sub CreateSubMain()
Dim filename As String
Dim fp As Integer
Dim project_name As String
project_name = Trim$(AddInDialog.ProjectNameText.Text)
Create the file SubMain.BAS.
fp = FreeFile
filename = App.Path & \SubMain.BAS
Open filename For Output As fp
Print #fp, Attribute VB_Name = SubMain
Print #fp, Option Explicit
Print #fp,
Print #fp, #If Win16 Then
:
<Code to declare GetPrivateProfileString and
WritePrivateProfileString omitted>
:
Print #fp, #End If
Create the Main subroutine for VB 4.
Print #fp, Sub Main()
Print #fp, Dim section As String
Print #fp, Dim val As String * 256
Print #fp, Dim length As Long
Print #fp,
Print #fp, #If Win16 Then
Print #fp, section = Add-Ins16
Print #fp, #Else
Print #fp, section = Add-Ins32
Print #fp, #End If
Print #fp,
Print #fp, See if the add-in is in VB.INI.
Print #fp, length = GetPrivateProfileString(section, _
Print #fp, & project_name & .Connector, , val, _
Print #fp, 256, VB.INI)
Print #fp,
Print #fp, If not, add it.
Print #fp, If length = 0 Then _
Print #fp, WritePrivateProfileString section, _
Print #fp, & project_name & .Connector, 0, _
Print #fp, VB.INI
Print #fp, End Sub
Create the AddToINI subroutine for VB 5.
Print #fp, Sub AddToINI()
Print #fp, Dim section As String
Print #fp, Dim val As String * 256
Print #fp, Dim length As Long
Print #fp,
Print #fp, #If Win16 Then
Print #fp, section = Add-Ins16
Print #fp, #Else
Print #fp, section = Add-Ins32
Print #fp, #End If
Print #fp,
Print #fp, See if the add-in is in VBADDIN.INI.
Print #fp, length = GetPrivateProfileString(section, _
Print #fp, & project_name & .Connector, , val, _
Print #fp, 256, VBADDIN.INI)
Print #fp,
Print #fp, If not, add it.
Print #fp, If length = 0 Then _
Print #fp, WritePrivateProfileString section, _
Print #fp, & project_name & .Connector, 0, _
Print #fp, VBADDIN.INI
Print #fp, End Sub
Close fp
VBInstance.ActiveProject.AddFile filename
End Sub
Building ConnectAddIn and DisconnectAddInThe new add-ins Connector class contains the ConnectAddIn and DisconnectAddIn subroutines. These routines build and remove the menu items needed by the new add-in in the Add-Ins menu. CreateConnector creates the file Connector.CLS to hold the new class. It then writes some preliminary information into the file to identify it as a Creatable, MultiUse class named Connector. It creates global variables that the new add-in will use to manage the Add-Ins menu items and event handlers.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|