Working with Resources Files

Using resource files can help in organizing your application. This is done by putting all your icons, cursors, pictures, avi, wav and other files in a resource file. Because everything with in a resource file is included in the exe file you do not have to distribute these items with your application. I thought this would be a good time to learn how and when to use a resource file, as well as the pro's and con's of using a resource file in your project.

In order to create a resource file you will need a resource editor. VB5/6 and VC++ both come with a resource editor. However in VB depending on your installation it may not be loaded. Using VB6 you can click on the Add-In manager and select the resource editor from the menu. In VB5 however you have two choices, you can use the resource editor that comes with VB5. It can be found on the CD under tools, or if you register your copy of VB you can download a newer version for free on Microsoft's site. I suggest the later, since I've been told that the version that comes with VB5 is not so easy to use.

Visual Basic Resource Editor

Pro's and Con's

The advantage of using a resource file is that everything your application needs is in one place. Also a resource file can not be accidentally deleted by the user causing your program not to function correctly. As previously stated you do not have to worry about adding this file to your distribution. However on the down side because the resource file is included in the exe file it will increase the size of your application. Also resource files can not be changed once the application is compiled and any changes in the resource file requires you to recompile the application.

What goes in a resource file

You can put just about anything in a resource file, the rule of thumb, items that do not have to be changed and then save can be put into a resource file. While the built in functions allow you to work with icons, cursors, pictures and strings you can have custom files. This is why you can add wav and other files to a resource file. The demo below adds an icon, cursor, bmp, gif and Crystal Report to a resource file.

Adding a resource file to your project

Before we can add anything to the resource file you must first add a resource file to your project. If you are using VB right click on the project tree and select "Add" then "Resource File". In VC++ if you used the application wizard then the resource file is already included, all you have to do is click on the "ResouceView" tab. If you did not use the application wizard then to add a resource file to your project click "Insert" on the menu and select "Resource" then select the file you wish to add, i.e. bmp, icon, cursor etc.

Once you have add the resource file to your project you can start adding items. In VB just double click on the resource file, then select what item you wish to add. You have 5 choices, string, cursor, icon, bmp and custom. The previous 4 are self-explanatory the 5th is used for wav, avi and any file type that is not included in the first 4 groups. Once you select the file type a dialog box will popup asking you to add the file. Just find the file you wish to add double click the file and it will be added to your resource file. You can then keep the default ID which starts with 101, or you can double click on the file and name the file yourself, it takes a number or string.

Copying files from the resource file

Private Sub Command1_Click()
Screen.MousePointer = vbHourglass
'Load the report into a temp directory
LoadDataIntoFile 103, "C:\windows\temp\testReport.rpt"
'Show the report
Report1.ReportFileName = "C:\windows\temp\testReport.rpt"
Report1.WindowTitle = "Test Report"
Report1.Action = 1
Screen.MousePointer = vbDefault
End Sub

Private Sub Form_Load()
Dim res
Picture1.Picture = LoadResPicture(101, vbResBitmap)
testRes.MousePointer = 99
testRes.MouseIcon = LoadResPicture(101, vbResCursor)
'Get the custom file from the res file
LoadDataIntoFile 101, "C:\windows\temp\comp5.gif"
LoadDataIntoFile 102, "C:\windows\temp\bul066.gif"
'Load the file into the picture box
Picture2.Picture = LoadPicture("C:\windows\temp\comp5.gif")
Picture3.Picture = LoadPicture("C:\windows\temp\bul066.gif")
'Delete the gif files from the temporary directory
Kill "C:\windows\temp\comp5.gif"
Kill "C:\windows\temp\bul066.gif"
End Sub

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

Since the resource editor doesn't have a built in command to handle gif, wav, rpt files, in order to add these type of files to the resource file you need to use the LoadResData command. This is done by using the LoadDataIntoFile function. As you can see I always try and clean up the files that I have loaded form the resource file by calling the Kill command.

You may also notice that when I load the files I use the LoadResPicture, with in the brackets two parameters are excepted. The first is the name or number of the file. When you first add a file the default is 101, 102, 103 etc. However the first parameter will take a number or string. The second parameter tells the resource editor what type of file it is, i.e. icon, cursor etc. When using the LoadDataIntoFile the first parameter is the same however the second parameter is the directory and path you want the resource file to copy the file to.

What does testRes.MousePointer = 99 mean?

When ever you use non standard mouse pointers you must use the 99 MousePointer . The MousePointer property has 15 constants if the pointer you are using is not a default then you need to use VBCustom or 99. ( see the MousePointer Property (ActiveX Controls) in the VB help file)

Conclusion

As you can see it's really easy to add and use a resource file in your project. You can download the VB version of this project at the link below.


VB Project File

Copyright© 2001 Marietta Crockett
Disclaimer