Dal's Programming Course - Lesson 06

Window Resources

It may surprise you to learn that Window executable files contain more than just binary machine instructions. Window executables can also contain all sorts of binary data that your program may use. These extra data items are called “resources”. One example of a resource is the icon that is used to represent your program on the desktop.

An alternative to using a resource, would be to store the icon in a separate file and access the file at runtime. The program would need to know the name of the file, and where the file is located to retrieve it. When the program is deployed on a user’s machine, the icon file would also need to be put on the user’s machine. Note that the icon's location now might be different -- so the program would have to have a way to find it.

Most Window programs use lot of data items such as icons. The concept of storing these items as resources makes working with such auxiliary data much more convenient than storing the data in files that are external to the program.

A resource is simply a data item that is identified with a number and stored in the program’s executable. You can ask Window to retrieve a resource by giving Windows the identification number. Resources are so useful that Microsoft has built in lots of support for them in Windows and in the Visual C++ .NET development system.

One of the difficulties with resources is managing all the identification (ID) numbers. To handle ID numbers, any smart programmer (i.e., you) would assign symbolic names to each ID number, and use the symbolic name instead of the raw ID number in the source code. Since the ID number will probably be used in many different *.cpp files, you would probably put all the IDs and their corresponding symbols in one include file, and then include that one file wherever you need it. Finally, whenever you create a new resource, you only need to edit this one file, add the symbolic name and a unique ID.

You should be glad to know that Microsoft has done all this for you. The development system maintains an include file, named “resource.h”. All you need to do, is to create the resource. The development system gives it a default symbolic name, and puts that name and a unique ID in the resource.h file. You, of course, can change the name of the symbol. You can also change the ID number, but I don’t recommend that, unless you really know what you are doing.

How do you create the resource? Well the development system has support for creating icons, bitmaps, menus, dialog box layouts, strings, cursors, and more. Learning how to create all this stuff is part of the fun of programming in Windows.

A few more nuts and bolts before you start: An *.rc file is the source file that describes the resources to be put in the executable. Some resources can be put directly in the *.rc file. The *.rc file is human readable. Originally, programmers were expected to edit this file. Now the editing of the *.rc file is done by the development system. A *.res file is created by the “resource compiler” from the *.rc file. The resource compiler will automatically run if your project has an *.rc file. You can add an *.rc file by simply creating a resource in the Resource View. To get the resource added correctly to your final executable file, you sometimes need to Rebuild, instead of just building. (I think this may be a bug in the development system.)

Materials for this Lesson: Download.
Homework

Item

Time

Description

1 10 Mins Create the program's Icon. By default, Windows assumes that the first icon it finds in the your executable is to be used as the program's icon. So, all you have to do is to create an icon and recompile all your code. No code changes are necessary! Your icon should be 32x32 pixels. Use the drawing tools inside of the development system. A happy face makes a good icon.
2 5 Mins Verify that Windows is using your icon. Do this by creating a shortcut to your program on the desktop. Beware that Windows cashes icons (in some weird place), so you might have to delete your executable and rebuild it, and run it before Windows will pick up and start using the new icon.
3 5 Mins Change the icon's name to IDI_SMALL_ICON instead of the default name (IDI_ICON1). Recompile again. Can you determine what ID number was assigned to the icon?
4 10 Mins Use the development system to open up the *.rc file in text edit mode. Try to understand the source in this *.rc file. Also open up resource.h. Then use windows explorer to list all source files for your program. Notice "lesson.rc", "resource.h", and "icon1.ico." Can you explain what is going on with all three files? That is, why does each exist. Look into the Debug directory (where the results of building your program are put). Can you explain what lesson.res is doing there, but why icon1.ico is not there?
5 5 Mins Close the text windows holding resource.h and lesson.rc. Make another icon. Then, reopen (in text mode) resource.h and lesson.rc. What changed? List all the source files once again... Are there any new *.ico files?
6 2 Mins Hopefully, at this point you can appreciate how nice the development system is to manage all the resource issues. It maintains some input files (i.e, "source" files). It also compiles those files into the output directory, and links the binary data in your executable. All this frees you to just develop the resources and not have to worry about the ID numbers and updating all the related files.