A variety of use skills of resource files in Visual Basic
Wuhan Aijun
Abstract This article introduces a variety of use techniques in Visual Basic's resource files: 1 Develop a Chinese and English (Jane, Fan) Dual Version Tips; 2 Implement "Green" software; 3 direct playing sound files; 4 Save all kinds of icons, cursor pictures and many more.
Key words resource file
First, develop Chinese and English (Jane, Fan) Double Version
Nowadays, more popular software will have multiple versions of Chinese, English, traditional, etc., and can automatically display the adaptive version according to the system language, how this feature is implemented in VB:
1. First, Judgment the language of the system through the API function getSystemDefaultlcid
Option expedition
Dim Lang as string
Private Declare Function GetSystemDefaultlcid LIB "Kernel32" () AS Long
Private sub flow_load ()
Dim Localeid As Long
LocaleId = GetSystemDefaultlcid
Select Case LocaleID
Case & H404
Msgbox "Current System: Chinese Traditional", "Language"
Case & H804
Msgbox "Current System: Chinese Simplified", "Language"
LANG = "1"
Case & H409
MsgBox "The current system is: English", "language"
LANG = "2"
End SELECT
End Sub
2, then the program can load saved in the resource file (here we are using this method), registry, INI file, text file, or other string in other files to achieve the purpose of achieving a variety of languages.
(1) Launch VB6.0, load "Visual Basic 6 Resource Editor" in "Add Procedure → Add Program Manager"
(2) In the VB Resource Editor window, click on "Edit String Form" as follows:
Identification No. Chinese (China)
101 cases
"102" in Chinese
103 English
201 ENSAMPLE
202 Chinese
203 ENGLISH
(3) Save the resource file, write the following code: (Customize the function loadstring)
Sub loadstring ()
Me.caption = loading (int (LANG & "01"))))
Command1 (0) .caption = loading (Int (LANG & "02"))))
Command1 (1) .caption = loading (Int (LANG & "03"))
End Sub
⑷ How to make a simple Chinese and English version of the program, if the value of the LANG is set to 1, then Chinese, if the value of the LANG is set to 2, the English is displayed.
Second, achieve "green" software
Now very popular "green" software, you don't need any installer as long as an EXE file can run the program.
And in the programming in VB needs to call a third party's control (it is difficult to use the API function to get all the features), which requires a installer to pack the required controls, DLL files, or other files. Go in, then how to use the "green" software in the VB, please see: Example:
1, main program. EXE
Use the program written in VB6.0, call the Winsock control, if there is an error without running on the machine where VB5.0, 6.0 is installed, resulting in failure of the program, then if the general method is used, only this program uses VB5 .0, 6.0 installation procedures are packaged, otherwise only use 2 methods to solve.
2, registration program. EXE
With VB6.0, use the resource file method, encapsulate the Winsock control into this program, run this program before running the main program .exe, first run this program, get the system's Winddows / System Directory through the API function GetSystemDirectory, then Copy the Winsock control to this directory and use the shell to complete the registration of the control.
(1) Launch VB6.0, load "Visual Basic 6 Resource Editor" in "Add Procedure → Add Program Manager"
(2) In the VB Resource Editor window, click "Add Custom Information", add the Winsock Control (C: /WindDows/system/msWinsck.ocx), double-click the custom resource you just created, pop up the Edit Properties window, define as follows:
Type: "OCX"
Identification number: 101
Language: Chinese (China)
(3) Save the resource file, write the following code:
Option expedition
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "getsystemdirectorya" (Byval Nsize As long) As long
Const max_path = 260 Note: Define a sufficiently long string
Private sub flow_load ()
DIM Tempfile () as Byte
DIM Filenum as in
DIM Tempdir as string
Tempdir = getwinsysdir Note: Custom Function Getting System's Winddows / System Directory
TempFile = loadResdata (101, "OCX") Note: To load a number of possible types of data from the resource (.res) file, and return a BYTE array
FILENUM = Freefile
Open Tempdir & "/mswinsck.ocx" for Binary Access Write as #filenum Note: New file (copy the Winsock control to the specified directory)
Put #filenum, Tempfile
Close #filenum
Note: Shell "Regsvr32" & Tempdir & "/mswinsck.ocx", VBNORMALFOCUS Note: Registered control, there is a pop-up dialog
Shell "Regsvr32" & Tempdir & "/mswinsck.ocx / s", VBNORMALFOCUS Note: Register control, no pop-up dialog msgbox "Successfully, now this program can run normally !!", vbokonly, "registration control"
Unload me
End Sub
Public function getwinsysdir () Note: Defines a function of the directory where you read Winddows / System
DIM S AS STRING, Length As Long
S = string (max_path, 0) Note: Assignment
Length = getSystemDirectory (s, max_path) Note: s is the acquisition directory, max_path is the length
S = Left (s, INSTR (S, CHR (0)) - 1) Note: Remove extra spaces
Getwinsysdir = S
END FUNCTION
Note: Supplement: The value of S and Max_Path is casual,
Note: such as: DIM S AS String * 20
Note: Length = GetSystemDirectory (s, 20)
3, supplementary description
After saving the above two programs with VB6.0, use VB5.0 to reopen these two programs and compile as the main program .exe, registration file .exe. (Compiled programs with VB5.0, do not need any DLL files under Win98, 2000)
The program runs
4, program operation
Both the main program .exe, registration file .exe, copy it to any machine, first run the registration file .EXE registration third party control, then run the main program .Exe, there will be no mistakes, one. The "green" software is complete.
Third, directly play the sound file in the exe file
With resource files, you can play a sound file (WAV file) directly in the EXE file to make the program more professional.
(1) Launch VB6.0, load "Visual Basic 6 Resource Editor" in "Add Procedure → Add Program Manager"
(2) In the VB Resource Editor window, click "Add Custom Data", add any WAV file, and double-click the custom resource you just created, pop up the Edit Properties window, defined as follows:
Type: "Wave"
Identification number: 102
Language: Chinese (China)
(3) Save the resource file, write the following code:
Option expedition
Private Declare Function GetSystemDefaultlcid LIB "Kernel32" () AS Long
Private Declare Function SndplaySoundFromMemory Lib "Winmm.dll" Alias "SndPlaySounda" (LPSZSOUNDA AS ANY, BYVAL UFLAGS AS Long) AS Long
Public const SND_ASYNC = & H1 & H1 &
Public const SND_MEMORY = & H4 & H4 &
Private sub fascist5_click () Note: Play WAV files
DIM Barr () as Byte
Barr = loadingResdata (102, "Wave")
SNDPLAYSOUNDFROMMORY BARR (0), SND_ASYNC OR SND_MEMORY
End Sub
⑷ Press F5 to run this program, click the Command button, you can hear the sound of your selected WAV file.
Fourth, directly call various icons, cursor pictures
In a program, we may need to call multiple icons, cursors, picture files. Generally, we are mainly using the imagelist control, but the use of resource files is the best way (you can use one control).
(1) Launch VB6.0, load "Visual Basic 6 Resource Editor" in "Add Procedure → Add Program Manager"
(2) In the VB Resource Editor window, click "Add Cursor", "Add Icons", "Add Bitmap" to load the required files.
(3) The program code is as follows:
Me.icon = loadRespicture (101, vbresicon) Note: Load icon
Picture1.Picture = LoadRespicture (101, vbresbitmap) Note: Load bitmap
Command1.mouseicon = LoadRespicture (101, vbrescursor) Note: Load cursor
5. If there are all the use skills of this resource file, I hope that these techniques are helpful to everyone's programming. All of the above programs are running in Win98, VB5.0 or 6.0. If you have any questions, Let us discuss with www.d1vb.com.