Creating a Modeless Dialog Box with MFC Libraries

Last reviewed: October 10, 1997
Article ID: Q103788
1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00
WINDOWS             | WINDOWS NT
kbprg

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and 1.52
        - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, and 4.0
    

This article demonstrates how to create a modeless dialog box using a dialog-box template resource using MFC.

To build a CModeless dialog box, the following steps should be followed:

  1. Create a dialog class derived from CDialog and override the Create() member function of CDialog. For example, you might have code that resembles the following:

          class CModeless: public CDialog
    
               {
                 .
                 .
                 .
                 public:
                     CModeless(){ }
                     BOOL Create(UINT nID, CWnd * pWnd)
                          { return CDialog::Create(nID,pWnd);}
                 .
                 .
                 .
               };
    
       NOTE: You could let ClassWizard generate the dialog class and then
       simply add the Create() function.
    
    

  2. Write code to construct an object of the dialog class and create the dialog box window. For example, if you have a menu item that should display the dialog box, you might have code that resembles the following:

          void CMainFrame::OnModeless()
    
            {
                pdlg = new CModeless;
                pdlg->Create(IDD_DIALOG1,this);
            }
    
    
where CMainFrame::pdlg is defined as:
          CModeless * pdlg;

   NOTE: It is important to allocate the object on the heap rather than
   the stack if you want to prevent the modeless dialog box from being
   destroyed when the function is exited.

  • You must call CWnd::DestroyWindow() in OnCancel, or whenever the modeless dialog box needs to be destroyed.

          void CModeless::OnCancel()
          {
    
              DestroyWindow();
          }
    
    

  • Override CWnd::PostNcDestroy() in your dialog class to delete the dialog box object:

    virtual void CModeless::PostNcDestroy() {delete this;}

    PostNcDestroy() is a virtual member function of the CWnd class that is called by the OnNcDestroy() function.

    A modeless dialog class will typically override the OnOK() and OnCancel() member functions to call DestroyWindow() and should not call the base class CDialog::OnOK() and CDialog::OnCancel() functions. The CDialog::OnOK() and CDialog::OnCancel() functions call EndDialog(). EndDialog() should be called only when using modal dialog boxes.

    If you are using Dialog Data Exchange (DDX) and Dialog Data Validation (DDV), you're OnOK() handler for your dialog class might resemble the following:

       void CModeless::OnOK()
       {
               if (!UpdateData(TRUE))
                 {
                   TRACE0("UpdateData failed during dialog termination\n");
                   // The UpdateData routine will set focus to correct item
                   return;
                 }
               DestroyWindow();
       }
    

  • Additional reference words: kbinf 1.00 1.50 2.00 2.10 2.50 2.51 2.52 3.00
    3.10 4.00
    KBCategory: kbprg
    KBSubcategory: MFcUI
    Keywords : MfcUI kbprg
    Technology : kbMfc
    Version : 1.00 1.50 1.51 1.52 | 1.00 2.00
    Platform : NT WINDOWS


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: October 10, 1997
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.