Using an RC file

Q: Add icon, cursor, bitmap, and sound resources to a BCB project

Answer:

In this situation, there is more than one way to skin a cat. You can either use the C++Builder Image Editor, or you can use good old fashioned RC files (or you can use a mixture of the two).

Using the Image Editor (limited to bitmaps, icons, and cursors)

The Image Editor allows you to create bitmap, icon, and cursor resources. You can save tham as BMP, ICO, or CUR files respectively. The Image Editor also allows you to create groups of resources and save them directly in one RES file. The RES file is more useful because you can't add BMP, ICO, or CUR files directly to the project, but you can add RES files.

To start, launch the Image Editor. Select the File | New | Resource File (res) menu option. Save the new resource file in your project directory. Use the Image Editor to design the cursors, icons, or bitmaps that you need to use in your program. Keep these resources inside the RES file. For example, if you need a new bitmap, select the Resource | New | Bitmap menu option. This creates a new bitmap right inside the RES file, instead of creating a BMP file on your hard drive.

When you are done creating and editing your resources, make sure that each one has a logical name. Then save the RES file and go back to the BCB IDE.

You can add the RES file to your project using one of two different methods. The first way is to select the Project | Add To Project menu option. This menu opton brings up a file open dialog box. Locate and select the RES file. This inserts the RES file into the Project Manager (you can also add files to the project using the big plus button in the Project Manager).

The other way to add a RES file to the project is to use the #pragma directive. Here is an example:

#pragma resource "PICTURES.res"

Using an RC file

On the surface, the Image Editor might seem easier than using an RC resource file, but if you have to maintain a lot of bitmaps, and if you have to modify them frequently, you might agree that RC files are actually more convenient. RC files become a necessity when you need to add WAV files to your project. To use an RC file, follow these steps:

Step 1:Use a program to create the cursor, icon, and bitmap files that you need. There are lots of bitmap picture editors out there, and most are a lot more powerful than the picture editor in BCB (even Borland agrees, BCB was written with itself, but Borland has stated that the BCB splash screen was designed with PhotoPaint or PhotoShop). Icons and Cursors are more difficult because there are fewer editors available. You can use the Image Editor and save each resource using .CUR and .ICO extensions instead putting all of the resources into one.RES file. You could also create the resources with BC++ 5, the Resource Workshop from BC++ 4.5, or the resource editor from any other compiler, such as MS Dev Studio. However you do it, group all of your .ICO, .BMP, and .CUR files into your project directory.

Step 2:Create the RC file. To create the RC file, open a new file using the editor of choice. I prefer to use the Text object in the BCB Object Repository. Save the new file in your project directory and call it RESOURCE.RC. Add statements to the RC file for each resource that you need to add. Follow the format shown below:

#include "resource.rh" ID_SPLASH BITMAP "splash.bmp"ID_ABOUT BITMAP "about.bmp"ID_HAND CURSOR "hand.cur"ID_FOLDER ICON "folder.ico"ID_TYPE WAVE "type.wav"ID_SEARCHAVI AVI "search.avi"

The first column contains the resource IDs. These will be defined in RESOURCES.RH in the next step. The second column describes the type of resource. The last column is the filename of the resource.

Step 3:Create the header file for the RC file. Once again, create a new text file using the editor of your choice. Save the file as RESOURCE.RH and enter text that follows this pattern:

#ifndef RESOURCE_RH#define RESOURCE_RH #define ID_SPLASH 1000#define ID_ABOUT 1001#define ID_HAND 1002#define ID_FOLDER 1003#define ID_TYPE 1004#define ID_SEARCHAVI 1005#endif

Step 3:Add the RC file to your project. When you need to load a resource, #include the resource header file and load the resource based on its ID.

Note: The Image Editor is less than perfect when it comes to working with complex bitmaps that contain custom 256 color palettes. This is another good reason to hand craft an RC file.

Note: This FAQ only shows you how to bind resources to your program. I have covered how to load and use the resources in the FAQs listed below.

Q: Load bitmaps and icons from the programs's resources

Answer:

This FAQ assumes that you have already bound your bitmaps or icons to your executable. If you need help adding bitmaps or icons to a project, see the FAQ on adding icons, cursors, and bitmaps to a BCB project.

Наши рекомендации