When using third-party controls in VB programming, the program must be able to find the third-party controls used in the system directory or program directory. There are usually two methods: one is to make the control and program into a compressed package and distribute it. The disadvantage of this is that it may cause the control file to be lost during the program propagation process. The second is to create an installation program. The disadvantage is that the production process is cumbersome. This article describes how to save third-party controls in the program in the form of custom resource files. After the program runs and before using the controls, the used controls are generated into the program directory to complete the shelling of the controls from the executable file.
The specific steps are as follows:
Step 1: Copy the control to be used to the current project directory, right-click the control, select Properties, and note the file size. Note that you should write down the specific number in bytes, not how many K, for programming purposes.
Step 2: Reference and use the control normally.
Step 3: Create a new resource file and add it to the project. Add the third-party control used as a custom resource (CUSTOM) in the resource file editor. Use the default 101 for the resource number. If multiple controls are used, add them to resource items 101, 102... in the resource file respectively, and the shelling code must also be modified accordingly.
Step 4: Write the control shelling code to read the data of the resource file and generate the control in the current directory of the program.
The code is as follows:
Private Const OCXSIZE = 57344 'The size of the control to be generated is 57344Byte, and the name is CoolToolBar.ocx
Sub Main()
Dim Ocx() As Byte 'OCX is an array of type Btye
Dim Counter As Long
Ocx = LoadResData(101, CUSTOM) 'Read resource No. 101 in the custom resource into the array OCX
'Note that there is an error in the instructions for loading custom resources in Microsoft's help. The custom resource is identified as CUSTOM instead of the number 10 mentioned in the help.
If Right(App.Path, 1) = ($%$43%^#ASD#2@$#f$%^) Then 'Read the path of the program, determine whether it is the root directory and process it separately
'The program is in the root directory
If Dir(App.Path & CoolToolBar.ocx) = Then 'Is there a control in the program path? If not, the control will be generated
'Write (generate) the control (CoolToolBar.ocx) in binary mode to the directory where the main program is located
Open App.Path & CoolToolBar.ocx For Binary As #1
For Counter = 0 To OCXSIZE - 1 'Note that because it starts from 0 Byte, the file size - 1Byte is the final value
Put #1, , Ocx(Counter)
Next Counter
Close #1
End if
Else
'The program is not in the root directory
If Dir(App.Path & /CoolToolBar.ocx) = Then 'Is there a control in the program path? If not, the control will be generated
'Write (generate) the control (CoolToolBar.ocx) in binary mode to the directory where the main program is located
Open App.Path & /CoolToolBar.ocx For Binary As #1
For Counter = 0 To OCXSIZE - 1 'Note that because it starts from 0 Byte, the file size - 1Byte is the final value
Put #1, , Ocx(Counter)
Next Counter
Close #1
End if
End if
Form1.Visible = True 'The controls used in the main program have been generated, display the main form, and enter the main program.
End Sub
Note: Add the above code to the project as a module, and select the startup object as Sub Main in the project-project property settings, which is the above shelling code. Then compile and generate an EXE file, copy the EXE file to other computers that do not have the control installed, and run it to see if the control portability is achieved.