MODELESS DIALOG BOXES

At the beginning of this chapter, I explained that dialog boxes can be either ”modal“ or ”modeless.“ So far we've been looking at modal dialog boxes, the more common of the two types. Modal dialog boxes (except for system modal dialog boxes) allow the user to switch between the dialog box and other programs. However, the user cannot switch to another window in the program until the modal dialog box is destroyed. Modeless dialog boxes allow the user to switch between the dialog box and the window that created it as well as between the dialog box and other programs. The modeless dialog box is thus more akin to the regular popup windows that your program might create.

Modeless dialog boxes are preferred when the user would find it convenient to keep the dialog box displayed for a while. For instance, the Windows WRITE program uses modeless dialog boxes for the Find and Change dialogs. If the Find dialog box were modal, the user would have to choose Find from the menu, enter the string to be found, end the dialog box to return to the document, and then repeat the entire process to search for another occurrence of the same string. Allowing the user to switch between the document and the dialog box is much more convenient.

As you've seen, modal dialog boxes are created using DialogBox. The function returns only after the dialog box is destroyed. It returns the value specified in the second parameter of the EndDialog call that was used within the dialog box procedure to terminate the dialog box. Modeless dialog boxes are created using CreateDialog. This function takes the same parameters as DialogBox:

hDlgModeless = CreateDialog (hInstance, lpszTemplate, hwndParent,

lpfnDialogProc) ;

The difference is that the CreateDialog function returns immediately with the window handle of the dialog box. Normally, you store this window handle in a global variable.

Although the use of the names DialogBox with modal dialog boxes and CreateDialog with modeless dialog boxes may seem arbitrary, you can remember which is which by keeping in mind that modeless dialog boxes are similar to normal windows. CreateDialog should remind you of the CreateWindow function, which creates normal windows.