Invoking the Dialog Box

During processing of WM_CREATE, the program's instance handle is obtained (and stored in a static variable) and MakeProcInstance is called to create an instance thunk for the dialog procedure. The pointer to the instance thunk is also stored in a static variable:

hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;

lpfnAboutDlgProc = MakeProcInstance (AboutDlgProc, hInstance) ;

The MakeProcInstance function assures that AboutDlgProc obtains the correct data segment address for this instance of ABOUT1.

The program checks for WM_COMMAND messages where wParam is equal to IDM_ABOUT. When it gets one, the program calls DialogBox:

DialogBox (hInstance, "AboutBox", hwnd, lpfnAboutDlgProc) ;

This function requires the instance handle (saved during WM_CREATE), the name of the dialog box (as defined in the resource script), the parent of the dialog box (which is the program's main window), and the address of the instance thunk return from MakeProcInstance. If you use a number rather than a name for the dialog box template, you can convert it to a string using the MAKEINTRESOURCE macro.

Selecting ”About About1...“ from the menu displays the dialog box, as shown in Figure 10-2. You can end this dialog box by clicking the OK button with the mouse, by pressing the Spacebar, or by pressing Enter. For any dialog box that contains a default push button, Windows sends a WM_COMMAND message to the dialog box, with wParam equal to the ID of the default push button when Enter or the Spacebar is pressed.

The DialogBox function you call to display the dialog box will not return control to WndProc until the dialog box is ended. The value returned from DialogBox is the second parameter to the EndDialog function called within the dialog box procedure. (This value is not used in ABOUT1 but is used in ABOUT2.) WndProc can then return control to Windows.

Even when the dialog box is displayed, WndProc can continue to receive messages. In fact, you can send messages to WndProc from within the dialog box procedure. ABOUT1's main window is the parent of the dialog box popup window, so the SendMessage call in AboutDlgProc would start off like this:

SendMessage (GetParent (hDlg), . . . ) ;

If you have a lot of dialog boxes within your program, you may not want to create and save instance thunks for all of them. You can instead create instance thunks as needed and free them after DialogBox returns:

lpfnDlgProc = MakeProcInstance (AboutDlgProc, hInstance) ;

DialogBox (hInstance, "AboutBox", hwnd, lpfnDlgProc) ;

FreeProcInstance (lpfnDlgProc) ;