Click here to Skip to main content
13,199,852 members (66,705 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 27 Feb 2000

Implementing Modal Message Loops

, 27 Feb 2000
Rate this:
Please Sign up or sign in to vote.
An introduction to creating modal windows


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.


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)

  MSG msg;
      if(msg.message == WM_QUIT)
        fDone = TRUE;


  if(hwndParent != NULL)


  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.


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


About the Author

Chris Becke
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThere are some typos. Pin
Anonymous3-Apr-03 2:18
sussAnonymous3-Apr-03 2:18 
GeneralRe: There are some typos. Pin
WREY11-Apr-03 6:36
memberWREY11-Apr-03 6:36 
GeneralGood Tip! Pin
Anonymous7-Aug-02 12:35
sussAnonymous7-Aug-02 12:35 
GeneralRe: Good Tip! Pin
Johann Gerell15-Mar-04 10:18
memberJohann Gerell15-Mar-04 10:18 
Anonymous wrote:
And why reposting the same message will do the trick?

Because fDone will then be set to TRUE and the modal loop exits before the WM_QUIT is handled again.

Dad, how strange it is that the pig can speak.
*thoughtful pause* It must have lost its "oink".
(my 3-year old daughter Moa, while watching Babe)

Human beings, who are almost unique in having the ability
to learn from the experience of others, are also remarkable
for their apparent disinclination to do so. (Douglas Adams)

GeneralModal dialogs Pin
Manjunath29-Apr-00 5:52
sussManjunath29-Apr-00 5:52 
GeneralRe: Modal dialogs Pin
Icymint329-Apr-04 14:08
memberIcymint329-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 28 Feb 2000
Article Copyright 2000 by Chris Becke
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid