Using Windows Resource Files

Level:
Level2

Download Visual Basic 6 source code for this tutorial

Resource files enable you to store bitmaps, waves, AVI's, icons, cursors, strings, and more... all in one file. It is to your advantage to use a resource file, the alternative is to leave all of your resources exposed, where the user can get at them and modify them. Once you've squished them into a resource file the user cannot touch them! HA!

So, to make a resource file you need a couple of things from your VB CD-ROM installation disk. Go root around in there and find the \Tools subdirectory of the main Visual Basic directory. Found it? Good. That RC.EXE file is what you need (have a look at the help files too). Copy it to your hard drive somewhere; it's just easier that way.

Now, you need to make a file called FILENAME.RC in a text editor. Change FILENAME to whatever you want the name of your resource file to be. Inside the text editor, you will describe which files you want to include in the resource, and how you want to refer to them in code. The syntax is:

nameID  keyword  filenamenameID

nameID

the name by which you will refer to this item in code.

keyword

a keyword defining what type of file this is. It can be BITMAP, CURSOR, ICON, SOUND, or VIDEO. SOUND is a .WAV file, VIDEO is an .AVI file.

filename

the path to the file on your hard drive.

Ok, for example, if I want to load a bitmap called VIEWSCRN.BMP into my resource file, and refer to that bitmap as ViewScreen in my code, I'd write:

ViewScreen BITMAP C:\BITMAPS\VIEWSCRN.BMP

We keep adding entries to the .RC file until we've included everything we wish to include. It'll end up looking something like this:

ViewScreen BITMAP C:\BITMAPS\VIEWSCRN.BMP
SpaceShip BITMAP C:\BITMAPS\SPACESHIP.BMP
Explosion SOUND C:\SOUNDS\EXPLOSION.WAV
CutScene VIDEO C:\AVIS\CUTSCENE.AVI

Once you're done, save the file and run the RC.EXE file on it. This program will gather all of the files you've listed and compile them into a file called FILENAME.RES. Use the command line (yes, I said command line... venture out into the world of DOS for a moment)

RC /r FILENAME.RC

... and it'll spit out the resource file for you. Now, go into your VB Project and add the resource file by pressing Ctrl-D and locating the file. Once you've included the resource in your project you can start using it in code. Use LoadResData or LoadResPicture to use the data you've stored in the resource file.

LoadResPicture("ViewScreen",0)

This command will return the bitmap VIEWSCRN.BMP that we stored in the resource file. We can then place it in a picture box control, or on a DirectDraw surface. The second argument for the LoadResPicture function determines what type of file we're accessing, 0 refers to bitmaps. 1 refers to icons, and 2 to cursors. Other types (Such as AVI's and WAV's) must be accessed through the LoadResData function.

Hopefully this Visual Basic tutorial was helpfull. Try storing different things in resource files and using them in your VB6 app. If you have any questions or comments post them below.

This tutorail is released under the GNU Free Documentation License 1.2. The original can be found here.

 

If you enjoyed this post, subscribe for updates (it's free)

cool but i need more

Dear author,
i would like to play a video in my app using .res file.

Nice tutorial

Awesome tutorial, .res files remained a mystery to me until i read this explanation. Great job!

Helpfull article

Thanks author,
I found your article very interesting.

I created a batch file that:
1) recreates the vb6-project resource file. This allow me to change the external file every time I build a new version of my program;
2) compile my vb6-project using vb6.exe /MAKE command.

My resource file will contain a MS-Access mdb that will be extract in a temporary random folder with a random-name.

Bye, Marco