|
Reference 1. Introducing Digital Mars C++ 2. Introducing the IDDE Part 2: Creating an Application with Digital Mars C++ 3. Starting a Project and Defining Workspaces 4. Generating an Application Framework 5. Defining Classes and Their Hierarchies 6. Editing Program Code 7. Adding Look and Feel with Resources 8. Testing an Application Part 3: Learning Digital Mars C++ by Example 9. Introduction to the Tutorial 10. Lesson 1: Create the DOS Application 11. Lesson 2: Generate an Application Framework 12. Lesson 3: Customize the Interface 13. Lesson 4: Add Messages with ClassExpress 14. Lesson 5: Add a Dialog Box with ClassExpress Part 4: More about Creating Programs 15. More about Projects and Workspaces 16. More about Project Build Settings 17. More about AppExpress 18. More about ClassExpress 19. Class Editor Reference 20. Hierarchy Editor Reference 21. Text Editor Reference 22. Using Version Control Part 5: More about Testing Programs 23. Controlling and Configuring the Debugger 24. Commands Available in Debugging Mode Part 6: About Managing Resources 25. ResourceStudio Resource Editor 26. Dialog Editor 27. Menu, Accelerator and String Table Editors 28. Bitmap, Cursor, Icon, and Font Editors 29. Version Information and Custom Resource Editors Part 7: Appendixes A. Expression Evaluation B. IDDE Settings and Command-Line Options C. Using NetBuild |
7. Adding Look and Feel with ResourcesAny application written for the Windows environment needs an interface to the user that gives it the "look and feel" of a Windows application. Various standard mechanisms are available to create this look and feel, and resources are some of the most important of these. Resources such as menus and dialog boxes define the interface to an application.ResourceStudio contains tools for creating and modifying the resources. This chapter defines the different types of resources and explains how to use ResourceStudio to create three basic resources: menus, dialog boxes, and bitmaps. The final section describes how to work with the identifiers by which resources are referenced in the source code. For more information about ResourceStudio, see ResourceStudio Resource Editor. For more information on resources and programming with resources, refer to the following texts:
What Are Resources?Resources are structured data objects that define Windows user interface elements. When you choose a command from a menu or select options from a dialog box, for example, you are interacting with an application's resources. Predefined resource types include bitmaps, cursors, icons, fonts, dialog boxes, menus, accelerators, strings, and version information.An application's resources typically are defined in a resource script file (.rc). Each resource definition contains information and data relevant to that resource type, as well as the identifier by which the resource is referenced in the source code. Resource script files are created and edited with ResourceStudio. Resources are compiled by a special resource compiler and are linked to the Windows application separately from the application code. This modular design enables you to modify the appearance of an application without changing, or even accessing, the program source code. Separating resources from code also makes it easier to use the same resource definitions for multiple applications. Resource TypesCreate and edit the following resource types in ResourceStudio:BitmapsBitmaps are used by an application to display pictures on the screen. Each bit, or group of bits, in a bitmap represents one pixel of the image. Bitmaps may be any size you specify.CursorsCursors are small bitmaps, usually 32 x 32 pixels in size, that represent the mouse pointer. Cursor images may contain areas that are transparent, allowing the screen background to show through unused parts of the cursor image. Cursors also may contain inverted areas in which the screen background color is reversed.In addition to image information, a cursor resource also specifies the cursor's hot spot, the exact pixel in the image that maps to the mouse pointer's screen location. IconsIcons are small bitmaps, usually 32 x 32 or 16 x 32 pixels in size, that represent applications or actions within an application. For example, when you minimize a Windows application, an icon is displayed on the screen to represent the minimized program. Like cursors, icons may contain transparent and inverted areas.FontsA font resource is a collection of up to 256 bitmaps of identical height. A font typically represents a character set, but it also may be used for more efficient management of bitmaps not representing characters.Dialog boxesDialog boxes are windows that communicate information and receive user input. For example, a dialog box may let the user set program options, or alert the user to an error and ask how to proceed. To enter information and make selections in a dialog box, a user manipulates graphical elements called controls. Commonly used controls include buttons, check boxes, listboxes, textboxes, and scroll bars.A dialog box resource is a complex set of data describing each control in detail, as well as general properties such as the dialog box size and location. MenusMenus are hierarchical lists of program commands and options. When a user chooses an item from a menu, either an action is taken or another menu opens to provide additional commands. A menu resource identifies the available commands, specifies their individual styles and attributes, and determines the order in which they appear.AcceleratorsAccelerators are key combinations a user presses to perform a task in an application and are frequently used as shortcuts to menu choices. An accelerator resource contains a collection of key combinations and associated commands.StringsStrings are text that an application may use in window titles, menus, dialog boxes, error messages, and so on. A string resource is a table of strings.Version informationThe Version Information resource contains version information for Windows .exe and .dll files, such as version number, file description, company name, and copyrights.User-defined resourcesUser-defined resources contain data that you define and use in your program in any manner you choose. For example, you may want to attach a table of data or a block of text to your executable file. ResourceStudio allows you to include these types of data in your application's resources and to edit the data in hexadecimal format.Using ResourceStudioResourceStudio can be used to:
Starting ResourceStudioStart ResourceStudio using any one of several commands available in the IDDE's Resource menu:
To create a new resource file, choose Open from the Resource menu, and in the File Open dialog box click Cancel. Then, double-click on the ResourceStudio icon on the desktop. The Resource Studio Shell window opens (Figure 7-1).
[Figure 7-1 Resource Studio Shell window] The Shell window is ResourceStudio's main window and contains the main menu and toolbar. From this window, you can create and open resource files and set ResourceStudio preferences. Another window that will open as you work is the Property Sheet. The Property Sheet does not open immediately, but rather opens when it is first needed. While creating and editing resources, you can use the Property Sheet to view and edit properties of the currently selected object. (In this context, "object" refers to either a resource as a whole or to an element within a resource, such as a dialog box control or a menu item.) Some types of objects have multiple pages of properties; to switch between pages, simply click on the tabs. Creating a new resource fileResources are contained in resource files. Use ResourceStudio to create and edit most types of files that store Windows resources. For example, you can edit the resources of executable files even if you do not have the original source code.To create a new resource file, choose New from the File menu. The New File dialog box opens (Figure 7-2), listing the types of resource files that can be created with ResourceStudio.
[Figure 7-2 New File dialog box] Select the type of resource file you want to create (see Table 7-1). Note that resource script (.rc) and binary resource (.res) files usually contain more than one resource. If you are starting to create resources for a new application, .rc is likely to be your choice of file type. Resource scripts can contain any type of resource. Table 7-1 Resource file types File Type Contains Resource script (.rc) Any type or combination of resources in text format Binary resource (.res) Any type or combination of resources in binary format Icon (.ico) A single icon resource in binary format Cursor (.cur) A single cursor resource in binary format Bitmap (.bmp) A single bitmap resource in binary format Font (.fnt) A single font resource in binary formatResources such as icons can be contained either in individual icon resource files (.ico) or with other resources in a resource script file. Some resources, such as dialog boxes and menus, are contained only in .rc or .res files. Thus, these resource options do not appear separately in the New File dialog box. The Platform option in the New File dialog box lets you create resource files that are compatible with Windows 3.1, Windows NT, or Windows 95. You can also elect to create resources for an MFC application by checking Support MFC. Warning: If dialog boxes for NT come out with a white background, select Windows 95 as the target platform. This is because the Windows NT target is for NT 3.51, an old version. After selecting a type of resource file, click OK. The window that opens next depends on the type of file you specified. If you selected one of the file types containing a single resource (icon, cursor, bitmap, or font), that resource type's editor opens in a separate window. If you selected Resource Script or Binary Resource, the Browser window opens (Figure 7-3).
[Figure 7-3 Browser window] The Browser window has three main areas:
Note: ResourceStudio has several subprograms, each of which is used to edit a particular resource type. In this chapter, for example, you will use the Menu editor, Dialog box editor, and Bitmap editor. Each individual resource editor can either be opened in a separate window or can use the right pane of the Browser window from which it is launched. The latter is the default behavior in most circumstances. Editing a resource fileBesides the resource files listed in the previous section, you can edit the resources of executable files, even though you don't have the original source code. By choosing Open from the Shell window's File menu, you can open files of any type listed in Table 7-1, plus files of the following types:
Creating a new menu resourceThe main interface between the user and a Windows application is generally the application's menu. The IDDE's Resource menu is a good example, as shown in Figure 7-4.
[Figure 7-4 A typical menu] A menu is a hierarchical structure containing pop-up items, menu items, and separators.
[Figure 7-5 Menu editor open in Browser window] Note: While a resource editor is open in the Browser window, the Browser window's menu is replaced by that of the particular resource editor. The editor's toolbar is displayed at the top of the right pane, below the Browser window's toolbar. The new menu resource initially contains a single pop-up item and a menu item. The list of items is indented to show the hierarchy. Pop-up items are labeled POPUP, menu items are labeled MENUITEM, and separators are labeled SEPARATOR. The currently selected item is enclosed in a box; to select any item, click on it. The Property Sheet shows properties of the selected item. The Test menu window (Figure 7-6) opens at the same time. You can test the menu in progress at any time in this window.
[Figure 7-6 Test menu window] Adding a pop-up itemStart creating a menu by adding a new pop-up item to the menu bar. To do so, click on Menu at the head of the item list. Then choose New Popup from the Menu menu. The new pop-up item is inserted at the start of the list.The Property Sheet displays the properties of the new pop-up item (Figure 7-7). At this point, you probably want to change only the menu name, which is contained in the Text field.
[Figure 7-7 Properties of a Pop-up menu] When changing the menu name, you can also assign an activation key to the menu. Put an ampersand (&) in front of the corresponding letter in the text. Adding a menu itemTo add one or more menu items to a drop-down menu, first make sure that the pop-up item is selected. Then choose New Item from the Menu menu. The new menu item is inserted into the hierarchy.The Property Sheet displays the properties of the new menu item (Figure 7-8). You can change the item's text and add an activation key. As with pop-up items, you add an activation key by typing an ampersand (&) in front of the corresponding letter in the text.
[Figure 7-8 Properties of a menu Item] The ID field contains the command ID, which is sent to the application when the user chooses this menu item. New command and resource IDs are assigned automatically to new objects when they are created; you can also change ID names and numerical values as desired. For more information on resource IDs, see the section "Managing Resource IDs" later in this chapter. Adding separatorsSeparators enhance the readability of menus. To insert a separator after the currently selected item, choose New Separator from the Menu menu.Editing menusThe normal editing operations (cut, copy, paste, and delete) work with items in a menu. Note that when you perform one of these operations on a pop-up item, all of the items it contains are affected as well.Rearrange menu items quickly by clicking and dragging. To move an item, drag the item to the location where it should be inserted. If you hold down the Control key while dragging, a copy of the selected item is inserted. The copy is given a new, unique resource ID. You can also rearrange menus with the arrow keys. To move an item up or down within its present level in the hierarchy, press Ctrl+Up Arrow or Ctrl+Down Arrow. To move an item horizontally, use Ctrl+Right Arrow or the Ctrl+Left Arrow. Closing the menu editorAfter you have arranged your new menu, close the Menu editor by choosing Close Editing from the File menu. The Browser window is updated to show the new resource (Figure 7-9).
[Figure 7-9 Browser window after creating menu resource] The Resource Types list contains an entry called Menu to show that you have at least one menu resource. The Resources list contains the ID of your menu resource. The Preview/ Edit area shows a preview of the new menu. The Property Sheet displays the menu resource's ID and memory options (Figure 7-10).
[Figure 7-10 Properties of a menu resource] To edit a menu resource, double-click on its ID in the Resource list, or select the resource and choose Edit Resource or Edit in Separate Window from the File menu. Creating menus with accelerators and item helpAs you create menus, you usually want to assign accelerator key combinations and item help strings at the same time. To do so, you must set up the string and accelerator tables before adding menu items, as outlined here:
To show which key combination is associated with each menu item, add the accelerator to the item text. For readability, the accelerator is usually separated from the command name with a tab. For example, if you want Ctrl+F to be the accelerator for a command called Find, enter Find\tCtrl+F into the Text field of the menu item's Property Sheet (Figure 7-11).
[Figure 7-11 Menu item text, showing accelerator] Simple guidelines for creating menusWhen creating a menu, keep in mind the following points:
Creating a new dialog resourceA dialog box is a window that communicates information and receives user input. It contains graphical elements called "controls." Figure 7-12 shows a typical dialog box.
[Figure 7-12 A typical dialog box] The following types of controls can be included in your dialog boxes:
[Figure 7-13 DialogExpress dialog box] DialogExpress gives you several options for simple dialog boxes that you may use as a starting point for your own dialog box. Select Standard and click OK. The Dialog editor opens in the right pane of the Browser window (Figure 7-14).
[Figure 7-14 Dialog editor open in Browser window] At the same time, the Dialog editor toolbox (Figure 7-15) opens. This toolbox provides shortcuts to commands in the Tool menu that let you create the different types of dialog box control.
[Figure 7-15 Dialog editor toolbox] The Property Sheet shows the General, Styles, and Look properties of the dialog box (Figure 7-16). You may want to start by changing the dialog box title. Type the new title into the Text field of the General properties page.
[Figure 7-16 Properties of a dialog resource] Adding a push buttonInitially, a new Standard dialog has no controls. To create a new push button:
[Figure 7-17 Dialog box resource with new push button] The new push button is selected and highlighted. Then move and resize the new push button control with the mouse.
The Property Sheet lets you modify push button properties (Figure 7-18). To change the button text, for example, change the Text field to the desired text. As with items in menus, you can set the activation key of a button or other control by typing an ampersand (&) before the corresponding letter in the text.
[Figure 7-18 Properties of a push button control] Adding static textTo add static text to a dialog box (for example, an informational message), choose Text from the Tool menu or click on the corresponding icon in the toolbox. Then click in your dialog box and drag the rubber-band box to the desired size.The Property Sheet shows the properties of the static text (Figure 7-19). To change the text, edit the Text field. After entering the text, you can resize the static control to the size of the text by dragging the handles.
[Figure 7-19 Properties of a static text control] To center the text within the control, select Center from the drop-down list in the Align/ Style property. Center the control within the dialog box by choosing Horizontal from the Center submenu of the Controls menu. A static text control may contain multiple lines of text. While typing text into the Caption field, press Ctrl+Enter to start a new line. Likewise, insert tab characters by pressing Ctrl+Tab. Testing the dialog boxTest the dialog box resource without leaving ResourceStudio. To open the new dialog box for testing, choose Test Dialog from the Dialog menu (Figure 7-20).
[Figure 7-20 Testing the new dialog box] When you are testing the dialog box, ResourceStudio does not function; you must close the test dialog box by pressing Alt+F4 before continuing. Aligning controlsCommands are available to align controls, to space them evenly, and to make them the same size. The following steps illustrate the three operations:
[Figure 7-21 Multiple selection of controls] Now you can perform any of the following alignment operations from the Controls menu:
Closing the dialog box editorAfter finishing with a dialog box, close the Dialog editor by choosing Close Editing from the File menu. The Browser window is updated to show the new resource (Figure 7-22).
[Figure 7-22 Browser window after creating dialog box resource] After creating a dialog box resource, you can use ClassExpress to create a corresponding dialog box class, set up the class's message map, and implement dynamic data exchange and validation. ClassExpress can be run directly from the Browser window's File menu. For further information, see Chapter 18, More about ClassExpress, as well as Chapter 13, Lesson 4: Add Messages with ClassExpress and Chapter 14, Lesson 5: Add a Dialog Box with ClassExpress. Creating a new bitmap resourceA bitmap is a picture that may be used for informational or decorative purposes. The Bitmap editor contains the functionality of a typical paint program, allowing you to create pictures of any size in 2, 16, or 256 colors.To create a new bitmap resource, choose New Bitmap from the Resource menu of the Browser window. The BitmapExpress dialog box opens (Figure 7-23).
[Figure 7-23 BitmapExpress dialog box] BitmapExpress allows you to set the number of colors and the initial size of the bitmap. If you are creating a bitmap for an MFC toolbar, you also can specify the number of buttons. For now, accept the defaults and click OK. The Bitmap editor opens in the right-most pane of the Browser window (Figure 7-24).
[Figure 7-24 Bitmap editor open in Browser window] The Bitmap editor window is divided into two panes. Draw in either. To change the relative sizes of the panes, click and drag the bar separating the panes. The Bitmap editor toolbox (Figure 7-25) also opens. This toolbox lets you choose graphics tools (also available in the Tool menu), set foreground and background colors, and select the line width and background pattern.
[Figure 7-25 Bitmap editor toolbox] The Property Sheet shows the General and Palette properties of the bitmap (Figure 7-26). Page between the two groups of properties by clicking on the tabs. The File field specifies the bitmap file; bitmaps are included in resource scripts by reference. If you want to change the bitmap file's name, type a new name into the File textbox.
[Figure 7-26 Properties of a bitmap resource] Changing bitmap sizeTo change the bitmap size, change the width and height in the Property Sheet or resize the bitmap directly by dragging one of the handles along the right and bottom edges of the bitmap.Selecting colors and patternsSelect drawing color and patterns from the toolbox in the following way:
Drawing toolsThese drawing tools are available in the toolbox or in the Tool menu:
Zoom and gridWork with an image at normal size or zoom by factors of 2, 4, or 8. To zoom, click in the pane containing the image, then select a Zoom command from the View menu. Set one image at normal size for reference and a second image at a larger size for easier drawing.To help finish the details of the image, you can display a pixel grid in the image. A pixel grid can be displayed only when the zoom factor is 4 or 8. To show a pixel grid, choose Grid from the View menu. Closing the bitmap editorAfter finishing with the bitmap, close the Bitmap editor by choosing Close Editing from the File menu. The Browser window is updated to show the new resource (Figure 7-27).
[Figure 7-27 Browser window, after creating bitmap resource] Useful ResourceStudio featuresA number of ResourceStudio features are useful for creating and editing different resources. These include toolbars, the undo and redo functions, and the dragging and dropping of items.Toolbars and pop-up menusResourceStudio makes extensive use of toolbars. Toolbar icons offer quick access to frequently used menu items. For a list of toolbar commands, see the "ResourceStudio Reference."In many parts of ResourceStudio, clicking with the right mouse button opens a pop-up menu. The contents of the menu depend on the item clicked and on the current mode or tool. Pop-up menus are a convenient way to access commands available from the main menu. Undo and redoResourceStudio can undo any operation or redo any operation that was undone. To undo the previous operation, choose Undo from the Edit menu or click on the Undo icon in the toolbar. Notice that the operation that will be undone is noted within the Undo menu item. To instead redo an action that was just undone, choose Redo from the Edit menu.Operations that you have performed are saved in lists. To set the number of operations that are saved (and thus that can be undone or redone), choose Preferences from the Shell window's Edit menu. Each Browser window and each resource type's editor keeps its own undo and redo list. To view a list of stored operations, click the right mouse button on the Undo or Redo toolbar icon. Select one or more operations from the list (see Figure 7-28).
[Figure 7-28 List of stored operations in Bitmap editor] Clipboard and drag and dropResourceStudio supports cut, copy, and paste of resources and of objects within resources, such as dialog box controls and menu items.ResourceStudio also supports drag and drop of all items that can be cut, copied, and pasted. To move a resource or resource element, click and drag it to the new location. For example, to move resources from one file to another, open a Browser window for each file, then drag the resources from the first file to the second. To copy rather than move the element, hold down the Control key until you have released the item. Managing Resource IDsWindows programs identify resources by a resource name, which can be a string or a number. Windows programs also use numbers to identify commands resulting from menu selections and accelerators and to identify dialog box controls. The number of identifiers used in a single application easily can run into the hundreds. One of the major features of ResourceStudio is its ability to automatically create and manage resource IDs.Resource ID fieldMany property pages contain an ID field for the current resource or resource element. There are three types of resource IDs in ResourceStudio:
Textual IDsTextual IDs are allowed only for certain resource types. To specify a textual ID, enclose the text in the ID field in double quotes. ResourceStudio warns you if you try to assign a textual ID to a resource type for which it is not allowed.Symbolic IDsSymbolic IDs are names that correspond to numbers. By using this type of ID, you work with names that are meaningful, leaving ResourceStudio to keep track of the numbers that match the names. Symbols and their corresponding numbers are saved in the resource header file as #define statements. Thus the same symbols can be used to refer to the resources in your application code.Assign a symbolic ID either by typing the name into the ID field or by selecting a pre-existing ID from the drop-down list. If you enter a new symbol, ResourceStudio automatically assigns a unique numeric value. If you want to specify the numeric value, follow the symbolic name with an equal sign and the value (for example, IDD_ TEST= 451). Reassign the value of an existing symbol in the same way. Numeric IDsNumeric IDs should not be used in new resource script (.rc) files; use symbolic IDs instead. If you create a binary resource file (.res), or edit the resources in an existing .exe or .dll file, you see the resource IDs as numbers, because the symbolic information is not saved for these file types. Assign a numeric ID by typing the number into the ID field.Note: When editing resources in .exe or .dll files, remember that the compiled source code refers to resources and commands by the numeric (or textual) ID; reassigning or changing IDs may result in incorrect behavior. Automatic creation of resource IDsResourceStudio automatically creates a new resource ID every time one is needed. The new ID is unique within its context: for resource elements, the ID is unique within the resource and, for resources, unique within the resource file.When assigning values to new symbols typed in by the user or created automatically, ResourceStudio uses different ranges for various purposes:
Resource ID browsingTo browse and modify resource IDs, choose Edit Resource IDs from the File menu.The Resource ID Browser dialog box opens (Figure 7-29).
[Figure 7-29 Resource ID Browser dialog box] The upper listbox shows all the symbolic resource IDs in the resource file. Symbols can be sorted by name, value, or used/ unused state. Click on an ID to show a list of resources using the ID in the lower listbox. Open a resource shown in the lower listbox by double-clicking on it, or by selecting it and clicking View Usage. The remaining buttons are:
|