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