1.7.2 Creating a Dialog Box

A dialog box is created by using either the CreateDialog or DialogBox function. These functions load a dialog-box template from the application's executable file, and then create a pop-up window that matches the template's specifications. The dialog box belongs to the predefined dialog-box class unless another class is explicitly defined. The DialogBox function creates a modal dialog box; the CreateDialog function creates a modeless dialog box.

Use the WS_VISIBLE style for the dialog-box template if you want the dialog box to appear upon creation.

Dialog-Box Template

The dialog-box template is a description of the dialog box: its height and width, the controls it contains, its style, the type of border it uses, and so on. A template is an application's resource and must be added to the application's executable file by using the Resource Compiler.

Dialog boxes can be easily modified and are system independent, enabling an application developer to change the template without changing the source code.

The CreateDialog and DialogBox functions load the resource into memory when they create the dialog box, and then use the information in the dialog template to create the dialog box, position it, and create and position the controls for the dialog box.

The Resource Compiler takes a text description of the template and converts it to the required binary form. This binary form is added to the application's executable file.

Dialog-Box Measurements

Dialog box and control dimensions and coordinates are device independent. Since a dialog box may be displayed on system displays that have widely varying pixel resolutions, dialog-box dimensions are specified in system character widths and heights instead of pixels. Characters are guaranteed to give the best possible appearance for a given display. One unit in the x direction is equal to 1/4 of the dialog base width unit. One unit in the y direction is equal to 1/8 of the dialog base height unit. The dialog base units are computed from the height and width of the system font; the GetDialogBaseUnits function returns the dialog base units for the current display. Applications can convert these measurements to pixels by using the MapDialogRect function.

Windows does not allow the height of a dialog box to exceed the height of a full-screen window. The width of a dialog box is not allowed to be greater than the width of the screen.