Click here to Skip to main content
15,914,419 members
Articles / Desktop Programming / MFC

Create a Modeless Dialog Box as Child Window

Rate me:
Please Sign up or sign in to vote.
4.00/5 (9 votes)
9 Nov 2000 296.1K   3.3K   33   33
Simple step by step article explaining how to create a modeless dialog box as child window.

This article is part of the drag and drop interface samples.

  1. Serializing ASCII Data
  2. Modeless child dialog
  3. Modeless sibling dialog
  4. The drag source
  5. The MFC drop target
  6. The TBTextTarget class

To create the modeless dialog as sibling, follow this link.

Follow these steps:

  1. Create a new dialog resource and use the Class Wizard for making a new CDialog based class for it; let's call it CDropDialog.
  2. In your CFormView-derived class, add a (private) member variable of type CDropDialog* as a container for the modeless dialog class, let's call it m_pModeless. In the constructor of your view, make sure you initialize m_pModeless to NULL.
  3. In your appropriate message handler, let's call it OnModeless, do the following:
    C++
    void CInterfaceView::OnModeless() 
    {
        // Display the modal dialog box
        if (!m_pModeless)
            m_pModeless = new CDropDialog;
    
        if (!::IsWindow(m_pModeless->GetSafeHwnd()))
            m_pModeless->Create(IDD_DIALOG1, this);
    
        m_pModeless->ShowWindow(SW_SHOW); 
    }

In the destructor of the parent window, proof if the dialog has been closed and release the memory:

C++
CInterfaceView::~CInterfaceView()
{
    if (m_pModeless)
    {
        if (::IsWindow(m_pModeless->GetSafeHwnd()))
            m_pModeless->EndDialog(IDCANCEL);
        delete m_pModeless;
    }
}

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
Web Developer
Germany Germany
PhD Chemist,
programming with MSVC & MFC since 1996

Comments and Discussions

 
GeneralHELP!! SAME PROBLEM HERE TOO Pin
wondermind13-Oct-04 9:32
wondermind13-Oct-04 9:32 
GeneralIf I set the dialog style to Child, it is not enabled Pin
David Fleming14-Jun-01 22:15
David Fleming14-Jun-01 22:15 
GeneralRe: If I set the dialog style to Child, it is not enabled Pin
Close Network18-Jan-05 12:09
Close Network18-Jan-05 12:09 
GeneralRe: If I set the dialog style to Child, it is not enabled Pin
ZendLion31-Aug-17 20:02
ZendLion31-Aug-17 20:02 
GeneralOnModeless Pin
Julien4-Jan-01 11:35
Julien4-Jan-01 11:35 
GeneralRe: OnModeless Pin
Julien4-Jan-01 11:46
Julien4-Jan-01 11:46 
GeneralEndDialog()?... must be DestroyWindow(). Pin
15-Nov-00 20:22
suss15-Nov-00 20:22 
GeneralRe: EndDialog()?... must be DestroyWindow(). Pin
Leo Davidson16-Nov-00 0:47
Leo Davidson16-Nov-00 0:47 
Does anyone know if there's a good reason for dialogs to need two functions to close them, neither of which can be used in all cases?

It seems stupid to me. I've written a wrapper class which makes a note of whether it's modal or not and has a function which calls the right one and I don't understand why Microsoft didn't do this in the first place since it would avoid a lot of trouble, especially with windows which are made in both styles or which change from one style to the other during development.

(The wrapper does more than just that, FWIW. If I ever document it properly I'll put it up here in case it's useful.)

GeneralRe: EndDialog()?... must be DestroyWindow(). Pin
16-Nov-00 17:19
suss16-Nov-00 17:19 
GeneralRe: EndDialog()?... must be DestroyWindow(). Pin
I_dont_want_no_stinkin_alias29-May-03 5:01
I_dont_want_no_stinkin_alias29-May-03 5:01 
Questionm_pDlg? Pin
Some Guy14-Nov-00 23:12
Some Guy14-Nov-00 23:12 
AnswerRe: m_pDlg? Pin
5-Jun-01 22:30
suss5-Jun-01 22:30 
GeneralRe: m_pDlg? Pin
9-Feb-02 9:43
suss9-Feb-02 9:43 

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.