Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Implementing Modal Message Loops

Rate me:
Please Sign up or sign in to vote.
3.50/5 (4 votes)
27 Feb 2000 84.7K   32   6
An introduction to creating modal windows

SUMMARY

The phrase modal window is used to describe a message or dialog box, that pops up over an applications frame window. While the modal window is present the application window cannot be used. The system provides the following functions to allow the creation of modal windows: MessageBox, DialogBox, DialogBoxParam, DialogBoxIndirect and DialogBoxParamIndirect.

MORE INFORMATION

There are two aspects to the implementation of a modal window. First, the parent window must be disabled, so as to prevent user interaction. Second, the modal message loop - a modal window is implemented with its own message loop. A typical implementation might look something like this:

BOOL fDone;
INT  nResult;

int RunModalWindow(
  HWND hwndDialog,
  HWND hwndParent)
{
  if(hwndParent != NULL)
    EnableWindow(hwndParent,FALSE);

  MSG msg;
  
  for(fDone=FALSE;fDone;WaitMessage())
  {
    while(PeekMessage(&msg,0,0,0,PM_REMOVE))
    {
      if(msg.message == WM_QUIT)
      {
        fDone = TRUE;
        PostMessage(NULL,WM_QUIT,0,0);
        break;
      }

      if(!IsDialogMessage(hwndDlg,&msg))
      {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
    }
  }

  if(hwndParent != NULL)
    EnableWindow(hwndParent,TRUE);

  DestroyWindow(hwndDialog);

  return nResult;
}

Note some important features of the loop:
The modal message loop cannot be terminated by calling PostQuitMessage as that function is used to terminate a UI thread. All windows owned by the thread must be destroyed. Thus, if WM_QUIT is picked up, it must be manually reposted.

The modal window is properly closed by calling a special function that sets a flag associated with the window. In the example code termination flag is fDone, and would usually be stored in a WindowLong. Usually a modal window also allows a return code - the example code uses nResult for this purpose.

Also note the order of the call to destroy the dialog and to enable the parent. The parent must be enabled before the dialog is destroyed, as disabled windows cannot recieve focus or activation. DestroyWindow will want to assign activation to a window - if the parent is disabled another top-level window will be chosen and activated - normally not the desired result.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThere are some typos. Pin
Anonymous3-Apr-03 2:18
Anonymous3-Apr-03 2:18 
GeneralRe: There are some typos. Pin
WREY11-Apr-03 6:36
WREY11-Apr-03 6:36 
GeneralGood Tip! Pin
Anonymous7-Aug-02 12:35
Anonymous7-Aug-02 12:35 
GeneralRe: Good Tip! Pin
Johann Gerell15-Mar-04 10:18
Johann Gerell15-Mar-04 10:18 
GeneralModal dialogs Pin
Manjunath29-Apr-00 5:52
Manjunath29-Apr-00 5:52 
GeneralRe: Modal dialogs Pin
icymint329-Apr-04 14:08
icymint329-Apr-04 14:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.