Working With Resource File Lesson 1 What is Resource File? Resource file is file that can contains multiple image files (BMP, GIF, JPG, ICO and more), Cursors (CUR), Sound files and other files. All these files can be in single Resource file, and you can access them from your program. For example, you can load an icon from resource file to your Command Button. Resource file has RES extension. Why should I use Resource File? Resource file is very useful when you use the same image several times in your code. For example, you have two Command Buttons with the same icon or two Picture Boxes with the same BMP picture. If you won't use resource file, but simply add the same icon to both Command Buttons Picture property, the icon will be embedded in each of the Command Buttons. so actually, your icon will be saved twice, and your application file will be bigger. If you'll use resource file, you will have only 1 icon saved in your application. Launching the Resource Editor For making resource file, you have Visual Basic Add-In called Resource Editor. To launch it, From VB Menu choose Add-Ins->Add-In Manager (Image 1). Image 1:
Then select the VB6 Resource Editor, mark the Loaded/Unloaded check Box, and press OK (Image 2). Image 2:
You've just added the Resource Editor Add-In. To launch it, click on its icon in the menu (Image 3). Image 3:
And the Resource Editor will appear on your screen: Image 4:
Adding image Files to the Resource File To add BMP file to the resource file, click on the "Add Bitmap" icon in the resource editor menu (Image 5). Image 5:
Then select your BMP file and press Open. By default it will be saved under 101 ID (Image 6). Image 6:
To add cursor file to the resource file, click on the "Add Cursor" icon in the resource editor menu (Image 7). To add icon, click on "Add Icon" in the resource file menu (Image 8), And to add GIF or JPG file click on "Add Custom Resource" in the menu (Image 9). Image 7:
Image 8:
Image 9:
You can use the "Add Custom Resource" to add Sound Files, Text files, and any other files. Adding image Files to the Resource File (Continue) You can add as much files as you want. Every file has its own ID, so you will be able to access it by its unique ID. Bitmap and Icon, for example, are different resources, so they can have the same ID. But two Bitmaps or two Icons can't have the same ID. If you will add Bitmap, it will be saved in the resource file under the Bitmap "Folder" (It's not really folder, because it's one single file), If you will add Icon, it will be saved under Icon "Folder", and so on (Image 10).
Image 10:
In the example that appear in Image 10, I added 1 GIF file ("CUSTOM" Folder), 3 Bitmaps, 1 Cursor and 2 Icons.
Renaming the image ID You can rename the image ID by clicking on the image you want to rename (in image 10, for example, I've clicked on 101 in the CUSTOM "Folder") and then clicking on the properties Icon in the menu (Image 11).
Image 11:
Then, in the Id Text Box, enter the new image ID. The ID Doesn't have to be Number! It can be word, like "Cube" in the example below: Image 12:
After you enter the new ID name for this image, press the OK Button. Adding the Resource File to your Project After you've added several Image files to the resource file, save it by pressing the save icon in the menu (Image 13).
Image 13:
Choose your resource file name, and press Save. Now not only the resource file was saved, but it been added to your Project. To see it, From Visual Basic menu choose View->Project Explorer (Image 14), and by default, under the Related Documents folder (Image 15), you will see the file Project1.RES (or if you saved the resource file in other name, yourResourceFileName.RES). Image 14:
Image 15:
Adding the Resource File to your Project (Continue) Now you have this resource file in your project. But what if you'll want in the future to add this exactly resource file to your project? would you have to make it again? The answer is no.
To add this resource file to a new project, choose from VB Menu Project->Add New Resource File (Image 16) , select this file and press Open. Image 16:
Note that "Add New Resource File" menu item will appear only if you've added the Resource Editor AddIn (I've explained how to add the Resource Editor Add-In in page 2). Accessing BMP, ICO and CUR files from your Program Now you have the resource file with all your image files in it. But how can you access them from your program? Start a new Project, add 1 Picture Box (named Picture1) to your form, and add the following code to the form: Private Sub Form_Load() Picture1.Picture = LoadResPicture(101, vbResBitmap) End Sub This code will load the Bitmap that found under the Bitmap "Folder" in the resource file, with the ID 101, to the Picture Box. Explanation of this code: LoadResPicture function load a picture from the resource file.
101 - the ID of the picture you want to load. if there is no Bitmap in your resource file with the ID 101, an error will occur. If your picture ID is a word like "Cube" instead of number like 101, you should write: Picture1.Picture = LoadResPicture("Cube", vbResBitmap) Note that the "Cube" is in quotes, and when the ID was a number it was not in quotes. vbResBitmap - Because the picture you want to load is a Bitmap, and it's under the Bitmap "Folder" in the resource file. If you want to load an icon with the ID "myIcon", use LoadResPicture("myIcon", vbResIcon) and if you want to load an cursor with the ID "myCursor" use LoadResPicture("myCursor", vbResCursor) Summary: To load BMP, Ico or Cur file from resource file use: LoadResPicture ("MyImageID", vbRes...) Where vbRes...=vbResBitmap if it's Bitmap file, vbRes...=vbResIcon if it's Icon file, and vbRes...=vbResCursor if it's cursor file. Examples in the next page... Accessing BMP, ICO and CUR files from your Program - Examples The next example will show you how to add an icon to Command Button from resource file. Add an Icon to your Resource File (under the Icon "Folder") and rename its ID to "myIcon". Add 1 Command Button to your form (named Command1). Set the Command Button Style property to 1 - Graphical, so it will be able to display images. Add the following code to your form: Private Sub Form_Load() Command1.Picture = LoadResPicture("myIcon", vbResIcon) End Sub Run the program, and you will see that the icon is displayed on the Command Button. You can load multiple Icons on multiple Command Buttons, and load multiple Bitmaps into multiple Picture Boxes at the same time, as demonstrated below: Image 17:
Accessing BMP, ICO and CUR files from your Program - Examples (Continue) The next example will show you how to change your mouse cursor to other cursor that found in resource file. Add a Cursor to your Resource File (under the Cursor "Folder") and rename its ID to "myCursor". Set the form MousePointer property to 99 - Custom. Add the following code to your form: Private Sub Form_Load() Form1.MouseIcon = LoadResPicture("myCursor", vbResCursor) End Sub Run the program, and you will see that the mouse cursor is the cursor that found in your Resource File, under the "myCursor" ID.
That's the end of Lesson 1. For more advanced techniques using Resource File (Like loading GIF, JPG and Sound files from Resource file) go to Lesson 2.
Lesson 2 Accessing GIF and JPG files from your Program There is no Built-In option to load GIF and JPG files, There is no vbResGIF or vbResJPG. So to load these files you'll have to use the following Function: Public Sub LoadDataIntoFile(DataName As Integer, FileName As String) Dim myArray() As Byte Dim myFile As Long If Dir(FileName) = "" Then myArray = LoadResData(DataName, "CUSTOM") myFile = FreeFile Open FileName For Binary Access Write As #myFile Put #myFile, , myArray
Close #myFile End If End Sub What does this function do? The function gets two parameters: DataName and FileName. It simply copy the File that found in the resource file, under the CUSTOM "Folder" With the Id that found in DataName variable. The new file name will be the String that found in the FileName variable. For example, assume I have a resource file, with EXE file that found under the CUSTOM "Folder". The EXE file ID is 101. Calling to: LoadDataIntoFile 101, "c:\MyDir\myFile.ddd" Will copy the EXE file to c:\MyDir\myFile.ddd It doesn't matter if the file is EXE, GIF, JPG, TXT or WAV. Because of that, this function can extract any file from resource file, and can be used to extract Sound files, Text files, and other files.
How the LoadDataIntoFile Function works? Public Sub LoadDataIntoFile(DataName As Integer, FileName As String) Declaring myArray and myFile variables Dim myArray() As Byte Dim myFile As Long If Dir("D:\Myfile.Exe") = "" Return True if the file exist and False if doesn't. If the file exist we don't start the copying process, to avoid overwriting an existing file. If Dir(FileName) = "" Then LoadResData loads file from Resource file that founds under the CUSTOM "Folder", like LoadResPicture loads Picture from resource file that found under all other Folders (Icon, Cursor and Bitmap) In the line below, we load the File that found under the CUSTOM "Folder" with the ID that the DataName variable holds, to the variable myArray myArray = LoadResData(DataName, "CUSTOM") FreeFile is a function that returns the next Free File Number If we wouldn't use FreeFile, and instead we were using: Open FileName For Binary Access Write As #1 And In our code there was before similar line like: Open "Autoexec.Bat" For Binary Access Write As #1 There was a collision because they were both opened As #1 The FreeFile function prevents that and in the example above would open
The second file as #2 myFile = FreeFile Opens new file with the name that founds in the FileName variable. Open FileName For Binary Access Write As #myFile Put the myArray variable data into the new file. Remember that the myArray variable holds right now the data of the file we want to copy Put #myFile, , myArray Close the file Close #myFile End If End Sub
Accessing GIF and JPG files from your Program - Example This example will show you how to use the LoadDataIntoFile function to load GIF or JPG file from resource file into Picture Box. Add 1 Picture Box to your form (named Picture1) and add 1 GIF or JPG file to your Resouce File. Set the file ID to be 101. Copy the following code to your form: Public Sub LoadDataIntoFile(DataName As Integer, FileName As String) Dim myArray() As Byte Dim myFile As Long If Dir(FileName) = "" Then myArray = LoadResData(DataName, "CUSTOM") myFile = FreeFile Open FileName For Binary Access Write As #myFile Put #myFile, , myArray Close #myFile End If End Sub Private Sub Form_Load() 'this will copy the GIF/JPG file to c:\tmpfile.$$$ LoadDataIntoFile 101, "c:\tmpfile.$$$" 'this will load the c:\tmpfile.$$$ file to Picture1 Picture Box Picture1.Picture = LoadPicture("c:\tmpfile.$$$") End Sub Run the program. The Gif/JPG file will be displayed in the picture Box.
Play Wav file that found in Resource File This example will show you how to play wav file that found in Resource File, using the LoadDataIntoFile function. Add 1 Wav File to your resource file, and set the Wav file ID to be 101. Add the following code to your form: 'the API declaration that play Wav file Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long 'the LoadDataIntoFile function Public Sub LoadDataIntoFile(DataName As Integer, FileName As String) Dim myArray() As Byte Dim myFile As Long If Dir(FileName) = "" Then myArray = LoadResData(DataName, "CUSTOM") myFile = FreeFile Open FileName For Binary Access Write As #myFile Put #myFile, , myArray Close #myFile End If End Sub Private Sub Form_Load() 'copy the Wav file to c:\tmpfile.$$$ LoadDataIntoFile 101, "c:\tmpfile.$$$" 'Play the Wav file using the sndPlaySound API function sndPlaySound "c:\tmpfile.$$$", 1 End Sub Run the program. The wav file will be played. Summary Resource File is very useful when you have a lot of image files in your application, or if you want to decrease the size of your application file. The resource file is intended to deal with BMP, ICO, and Cur files, and you can easily load them from the resource file with Visual Basic Built-In functions. But with additional functions that copy the file from the resource file to the hard disk, you can use it with all the types of files.