Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Working with Resources

If you have been developing Windows applications for a while, you are probably familiar with resource scripts. These are files that define all of the resources used by your application, including dialog boxes, icons, and menus. In the old days, you often had to move back and forth between your source editor and your resource editor, the C compiler, and the resource compiler. Developer Studio makes this much simpler by providing a visual editor for your resources that is fully integrated with the rest of Developer Studio.

The Resource View

In programming for Windows, you will use several different types of resources. Most of these correspond directly to graphics objects such as bitmaps, cursors, and icons, as well as more complicated types such as menus, toolbars, and dialog box templates. In addition, you will have a string table and version resource, as well as any custom resources you might define.

In the bad old days, all resources were defined in a text resource script (.rc) file. You then had to compile the .rc file with a separate resource compiler and explicitly link your resources to your executable. Visual C++ 6.0 still uses the .rc file, but you will most likely never have to edit it directly. Developer studio allows you to edit your resources graphically, and will automatically take the necessary steps to compile your resource script and link it to your application.

To help you work with resources, Developer Studio provides the ResourceView, which displays information about the various resources included in each project, grouped by resource type. The ResourceView for the HiMom project is shown in Figure 1.10.

01fig10.gif

Figure 1.10 The Resource view.

If you created a Windows application with AppWizard, a resource script would automatically be created as part of your new project. However, you can also create a new resource script from the Files tab for the New command on the File menu or open existing re source scripts from the Open command on the File menu. Resource script windows support drag and drop, making it quite simple to open an existing resource script from another project and add resources to your current project by just dragging them over.

Like the other Project Workspace views, there is a context menu available by right-clicking in the ResourceView. This allows you to open the selected resource for editing, insert new resources, or import existing resources, among other things. In addition, the context menu gives you access to the Properties page for your resources, as shown in Figure 1.11.

01fig11.gif

Figure 1.11 Resource Properties dialog box.

Unlike some property pages, the resource property pages allow you to change some important aspects of your resource, including the ID and language for the resource. In addition, resources such as icons and bitmaps that are stored in files outside of the resource script, allow you to specify the filename for the resource here. The Condition field allows you to enter a precompiler symbol that is used to specify that the resource only be built into your project if the given precompiler symbol is defined.

Importing Resources

If you have already created resources that exist in separate files, you can add these to the resource script for the current project by importing them. You can add a separate image, icon, or cursor file to your current resource script by clicking the Import button in the Insert | Resource dialog box. This will allow you to browse for the files containing the resources you want to add to the current project.

Managing Resource IDs

Whenever you create a new resource with the Developer Studio, a resource ID is automatically assigned. This means that the symbol you have entered as the resource ID is defined to be a certain value in a header file. Although you might luck out and never have to deal with resource IDs or resource header files directly, The View menu offers two very useful commands: Resource Symbols and Resource Includes. The Resource Symbols command will produce the dialog box shown in Figure 1.12.

01fig12.gif

Figure 1.12 Resource Symbols dialog box.

Working with Resource Symbols

The Resource Symbols dialog box provides a handy way to work with the symbols that are defined for the currently selected configuration. You will see a list of all of the symbols defined in your project, their values, and whether or not they are currently being used by a resource. If a symbol is in use, the Used by list will show the resources using that symbol. Selecting Show read-only symbols will display predefined windows symbols in a lighter shade along with your symbols.

You can add new symbols by clicking the New button or change values with the Change button. The View Use button will open a window for directly editing the resource that uses the selected ID. To change the value of a symbol that is in use, use the View Use button to open a window for the resource. If you open the properties dialog box for the resource, by right-clicking in the resource and choosing Properties, you can assign a value to the symbol by entering it after the symbol name, like this:

IDD_MYDIALOG=111

Working with Resource Includes

The Resource Includes command from the View menu will display a dialog box like that shown in Figure 1.13. This dialog box allows you to work with the include files that define your resource symbols.

01fig13.gif

Figure 1.13 Resource Includes dialog box.

You may change the name of the header file that will contain your resource symbol definitions here. You may also add any #include or #define directives to the header file. This allows you to include additional header files, which can help in group projects. Because none of the Developer Studio tools will modify the #included files, these values are much more likely to remain constant.

Resource Templates

You might have noticed that when you create a new resource, the tree list of resource types available may be expanded, as in Figure 1.14. This provides you with the ability to create resources based on resource templates. This comes in handy whenever you will want to create many different resources that are similar, and can help standardize the look and feel of your applications.

01fig14.gif

Figure 1.14 Creating resources with resource templates.

You can create resources based on the template by selecting the template and clicking New. If you just want a plain, blank dialog boxes, select the dialog box line before clicking New.

With Developer Studio, you can create your own resource templates by choosing Resource Template from the Files tab of the New dialog box displayed by the File | New command. This will open an empty resource script that you can insert resources into. When you are finished, a resource template (.rct) file will be created. When this file is saved in the Microsoft Visual Studio\Common\MSDev98\Template subdirectory with the other templates, Developer Studio will read the template file whenever the Insert Resource dialog box is displayed. If you create a custom resource template and move it into the template subdirectory, you will see that your resources defined in your template are shown among the choices in the Insert Resource dialog box tree. Whenever you create a new resource based on the template, any changes you make to the new resource affect only the new resource and not the template.

Share ThisShare This

Informit Network