Click here to Skip to main content
15,914,488 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

 
QuestionModeless Dialog boxes Pin
ksrikant832-Jul-08 19:42
ksrikant832-Jul-08 19:42 
AnswerRe: Modeless Dialog boxes Pin
Pandele Florin18-Jan-11 3:01
Pandele Florin18-Jan-11 3:01 
Questionare you suppose to be able to type data in text box?? Pin
doctorrie23-May-05 8:30
doctorrie23-May-05 8:30 
AnswerRe: are you suppose to be able to type data in text box?? Pin
cristitomi22-Mar-07 4:32
cristitomi22-Mar-07 4:32 
GeneralControls on child dialog are not enabled - Solution! Pin
Member 121663229-Mar-05 16:15
Member 121663229-Mar-05 16:15 
GeneralIf this doesn't work for you... Pin
Member 121663229-Mar-05 16:24
Member 121663229-Mar-05 16:24 
Generaldialog style as Child but always disabled Pin
wondermind13-Oct-04 12:20
wondermind13-Oct-04 12:20 
GeneralSee "Solution", 30 March 2005. nt Pin
Member 121663229-Mar-05 16:19
Member 121663229-Mar-05 16:19 
GeneralRe: dialog style as Child but always disabled Pin
cristitomi22-Mar-07 4:33
cristitomi22-Mar-07 4:33 
GeneralModeless dialoge Pin
sohcher26-Feb-04 10:20
sohcher26-Feb-04 10:20 
Generalusing database Pin
vishalbhatara4-May-03 23:49
vishalbhatara4-May-03 23:49 
QuestionHow 2 call a dialog using a button Pin
Biswakalyan16-Jun-02 23:05
Biswakalyan16-Jun-02 23:05 
AnswerRe: How 2 call a dialog using a button Pin
Anonymous2-Nov-02 5:07
Anonymous2-Nov-02 5:07 
Generalfront window Pin
tammari8-May-02 21:20
tammari8-May-02 21:20 
GeneralRe: front window Pin
WalkerLan12-May-02 23:19
WalkerLan12-May-02 23:19 
GeneralRe: front window Pin
DarkCloud149-Dec-03 11:39
DarkCloud149-Dec-03 11:39 
GeneralDetected memory leaks!! Pin
8-Apr-02 22:11
suss8-Apr-02 22:11 
GeneralJumping between several dialogs Pin
29-Jul-01 12:57
suss29-Jul-01 12:57 
GeneralHEELLLPPPPP Pin
10-Jul-01 9:14
suss10-Jul-01 9:14 
GeneralRe: HEELLLPPPPP Pin
10-Jul-01 9:44
suss10-Jul-01 9:44 
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 
In the message handler OnModeless there are compilcations.

I had a CListControl taking up all the space on the 2nd dialog and
in the method OnKillfocusList(NMHDR* pNMHDR, LRESULT* pResult) I
added this->DestroyWindow(); (Which produced the PostNcDestory etc etc)

However when I tried to Create(ID_2ND_DIALOG, this) the 2nd time I got a
DebugBreak which I could not locate in the source. I went down the whole
path of just re-using the first instance of the Dialog (so Create is only
called once) but I didn't like it.

So, I added this :
const int WM_CUSTOM_DESTROY = WM_USER+100;

void C2ndDlg::OnKillfocusList(NMHDR* pNMHDR, LRESULT* pResult)
{
*pResult = 0;
// this->DestroyWindow(); We don't want to call this any more.
this->PostMessage(WM_CUSTOM_DESTROY);
}

void C2ndDlg::OnCustomDestroy(UINT, UINT)
{
this->DestroyWindow();
}

and to the MessageMap
ON_MESSAGE(WM_CUSTOM_DESTROY, OnCustomDestroy)

Suddenly it works!
Which allows me to NOT maintain all the pointer to the C2ndDlg objects
and instead add 'delete this' to the OnPostNcDestory() as in MSDN.

FYI:
What this means is that stuff is happening After the OnKillfocusList
which needs to happen before we can call DestroyWindow, its just really
hard to find out what.
If anyone know can they please reply.

Thanks
Jules

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.