2.4.1 Creating a Dialog Box Template

A dialog box template is a description of a dialog box's style, contents, shape, and size. You can create your own custom template or use Microsoft Dialog Editor (DLGEDIT.EXE). In this example, the template is created manually. For information about how to use Dialog Editor to create a dialog box, see Microsoft Windows Programming Tools.

You create a dialog box template by creating a resource-definition file. This file contains definitions of resources to be used by the application, such as icons, cursors, and dialog box templates. To create an About dialog box template, you use a DIALOG statement and fill it with control statements, as in the following example:

AboutBox DIALOG 22, 17, 144, 75
STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU
CAPTION "About Generic"
BEGIN
 CTEXT "Microsoft Windows"        -1, 0,  5, 144,  8
    CTEXT "Generic Application"   -1, 0, 14, 144,  8
    CTEXT "Version 3.1"           -1, 0, 34, 144,  8
 DEFPUSHBUTTON "OK"              IDOK, 53, 59, 32, 14, WS_GROUP
END

The DIALOG statement starts the dialog box template. The name AboutBox identifies the template when the DialogBox function is used to create the dialog box. The box's upper-left corner is placed at the coordinates (22,17) in the parent window's client area. The box is 144 units wide by 75 units high. The horizontal units are 1/4 of the dialog box's base-width unit; the vertical units are 1/8 of the dialog box's base-height unit. The current base units are computed from the height and width of the current system font. The GetDialogBaseUnits function returns the dialog box's base units in pixels.

The STYLE statement defines the dialog box style. This particular style is a window with a framed border, a title bar, and a System menu, which is the typical style used for modal dialog boxes.

The BEGIN and END statements mark the beginning and end of the control definitions. The dialog box contains text and a default push button. The push button lets the user send input to the dialog box procedure to terminate the dialog box. The statements, strings, and integers contained between the BEGIN and END statements describe the contents of the dialog box. (Because you would normally create such a description by using Dialog Editor, this guide does not describe the numbers and statements that make up the description. For a complete description of how to use Dialog Editor, see Microsoft Windows Programming Tools.)

The CTEXT statement creates a rectangle with the quoted text centered in a rectangle. This statement appears several times, once for each of the various texts that appear in the dialog box.

DEFPUSHBUTTON creates a push button that allows the user to give a default response—in this case, choosing the OK button causes the dialog box to disappear.

The DS_MODALFRAME, WS_CAPTION, WM_SYSMENU, IDOK, and WS_GROUP constants used in the dialog box template are defined in the Windows header file. You should include this file in the resource-definition file by using the #include directive at the beginning of the definition file.

The statements in this file were created with a text editor and were based on a dialog box used in another application. You can create many such resources by copying them from other applications and modifying them by using a text editor. You can also create new dialog boxes by using Dialog Editor. (The files created by Dialog Editor contain statements that are somewhat different from the statements shown here, and such files usually are edited only by using Dialog Editor.)