working with resource file

15
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 :

Upload: abhilash-v-pillai

Post on 10-Apr-2015

1.252 views

Category:

Documents


1 download

DESCRIPTION

A small but detailed (including pictures) help on how to use resource files in your visual basic 6.0 applications. Shows how to edit a resource file, and insert different types of files (image, cursor, sound) in it and how to add the resource to the project. Very helpful if you have a lot of images/icons/cursors and sounds files in your project. Also helpful if you want to manage the size of the project.

TRANSCRIPT

Page 1: Working With Resource File

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 EditorFor 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:

Page 2: Working With Resource File

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:

Page 3: Working With Resource File

Adding image Files to the Resource FileTo 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:

Page 4: Working With Resource File

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:

Page 5: Working With Resource File

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 IDYou 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 ProjectAfter 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

Page 6: Working With Resource File

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:

Page 7: Working With Resource File

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 Add-In (I've explained how to add the Resource Editor Add-In in page 2).

Accessing BMP, ICO and CUR files from your ProgramNow 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

 

Page 8: Working With Resource File

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" useLoadResPicture("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 - ExamplesThe 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:

Page 9: Working With Resource File

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

Page 10: Working With Resource File

        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.dddIt 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 NumberIf we wouldn't use FreeFile, and instead we were using:Open FileName For Binary Access Write As #1And In our code there was before similar line like:Open "Autoexec.Bat" For Binary Access Write As #1There was a collision because they were both opened As #1The FreeFile function prevents that and in the example above would open

Page 11: Working With Resource File

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 filewe 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.

Page 12: Working With Resource File

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 filePrivate Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _     (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

'the LoadDataIntoFile functionPublic 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.$$$", 1End Sub

Run the program. The wav file will be played.

SummaryResource 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.