More on the Dialog Box Style

The window style of the dialog box is specified in the STYLE line of the dialog box template. For ABOUT1, we used a style that is most common for modal dialog boxes:

STYLE WS_POPUP | WS_DLGFRAME

However, you can also experiment with other styles. For example, you can try:

STYLE WS_POPUP | WS_CAPTION

This creates a dialog box with a caption bar and a normal window border. The caption bar allows the user to move the dialog box around the display by using the mouse. When you use WS_CAPTION, the x- and y-coordinates specified in the DIALOG statement are the coordinates of the dialog box's client area, relative to the upper left corner of the parent window's client area. The caption bar will be shown above the y-coordinate.

If you have a caption bar, you can put text in it using the CAPTION statement in the dialog box template:

CAPTION "Dialog Box Caption"

following the STYLE statement. Or while processing the WM_INITDIALOG message in the dialog procedure, you can use:

SetWindowText (hDlg, "Dialog Box Caption") ;

If you use the WS_CAPTION style, you can also add a system menu box with the WS_SYSMENU style:

STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU

This style allows the user to select Move or Close from the system menu.

Adding WS_THICKFRAME to the style allows the user to resize the dialog box, although resizing is unusual for a dialog box. If you don't mind being a little unusual, you can also try adding WS_MAXIMIZEBOX to the STYLE statement.

The STYLE statement is not required. If you do not include a STYLE or CAPTION statement in the template, the default style is:

WS_POPUP | WS_BORDER

But this is rather dull looking. WS_DLGFRAME produces much more attractive results. If you include a CAPTION statement with a STYLE statement, the default style is:

WS_POPUP | WS_CAPTION | WS_SYSMENU

You can also add a menu to a dialog box by specifying:

MENU menu-name

in the dialog box template. The argument is either the name or number of a menu in the resource script. Menus are highly uncommon for modal dialog boxes. If you use one, be sure that all the ID numbers in the menu and the dialog box controls are unique.

Although the dialog box window procedure is normally within Windows, you can use one of your own window procedures to process dialog box messages. To do so, you specify a window class name in the dialog box template:

CLASS "class-name"

This approach is rare, but we'll use it in the HEXCALC program shown later in this chapter.

When you call DialogBox specifying the name of a dialog box template, Windows has almost everything it needs to create a popup window by calling the normal CreateWindow function. Windows obtains the coordinates and size of the window, the window style, the caption, and the menu from the dialog box template. Windows gets the instance handle and the parent window handle from the parameters to DialogBox. The only other piece of information it needs is a window class (assuming the dialog box template does not specify one). Windows registers a special window class for dialog boxes. The window procedure for this window class has access to the pointer to your dialog box procedure (which you provide in the DialogBox call), so it can keep your program informed of messages that this popup window receives. Of course, you can create and maintain your own dialog box by creating the popup window yourself. Using DialogBox is simply an easier approach.