15

Can someone tell me how I could create a Non Modal Dialog in MFC's Visual c++ 6.0 and show it? I wrote this code:

CDialog dialog;
if (dialog.init(initialization values...))
   dialog.DoModal();

But it blocks my application from showing the dialog. I dont know if there exists any method or other way to do it.

Thanks

TBD
  • 509
  • 7
  • 15

6 Answers6

18
/* CChildDialog class is inherited from CDialog */
CChildDialog *m_pDialog = NULL;

// Invoking the Dialog
m_pDialog = new CChildDialog();

if (m_pDialog != NULL)
{
      BOOL ret = m_pDialog->Create(IDD_CHILDDIALOG, this);

      if (!ret)   //Create failed.
      {
         AfxMessageBox(_T("Error creating Dialog"));
      }    
      m_pDialog->ShowWindow(SW_SHOW);
}

// Delete the dialog once done
delete m_pDialog;
Fábio
  • 3,291
  • 5
  • 36
  • 49
Ramakrishna
  • 500
  • 1
  • 4
  • 11
  • delete m_pDialog; it could crash the application. you can call m_pDialog->DestroyWindow (); to delete dialog. – bala Sep 17 '14 at 06:11
  • 1
    Will the dialog be delete itself if I close it? – Vinz Nov 07 '14 at 10:30
  • According to MSFT, dialogs are not designed for auto-cleanup. But as long as you aren't doing anything fancy with memory or handle allocations, it will normally work. https://learn.microsoft.com/en-us/cpp/mfc/tn017-destroying-window-objects?view=vs-2019 – SChalice May 20 '19 at 20:13
5

You can call CDialog::Create and CWnd::ShowWindow like the others have suggested.

Also, keep in mind your dialog will be destroyed right after its creation if it is stored in a local variable.

phil
  • 182
  • 1
  • 7
5

Use CDialog::Create and then use CDialog::ShowWindow. You now have a modeless dialog box.

Goz
  • 61,365
  • 24
  • 124
  • 204
5

In this case I find it most convenient to let it self-delete itself to handle the cleanup.

Often it's considered bad form to make "implicit" memory freeing from within a class, and not by what it created it, but I usually make exceptions for modeless dialog boxes.

That is;

Calling code:

#include "MyDialog.h"

void CMyApp::OpenDialog()
{
    CMyDialog* pDlg = new CMyDialog(this);
    if (pDlg->Create(IDD_MYDIALOG, this))
        pDlg->ShowWindow(SW_SHOWNORMAL);
    else
        delete pDlg;
}

Dialog code:

void CMapBasicDlg::OnDestroy()
{
    CDialog::OnDestroy();
    delete this; // Shown as non-modal, we'll clean up ourselves
}
Jonas
  • 1,172
  • 1
  • 16
  • 26
  • 1
    This is closer to correct than the current top answer, but the delete should actually be done in OnNcDestroy. WM_DESTROY marks the start of the destruction sequence, WM_NCDESTROY marks the end. You want to wait until the latter (last message it will receive) to delete the MFC dialog. – Dennis Dec 18 '19 at 15:50
3

You need to call CDialog::Create instead. You will need to call DestroyWindow when you are finished with the dialog. You might also need to pass dialog messages onto the object but I can't remember if MFC handles this for you or not.

Rob
  • 76,700
  • 56
  • 158
  • 197
3

DoModal is blocking. You have to create your dialog on the heap or make it a member of your class (this is important), call Create then call ShowWindow.

Nikola Smiljanić
  • 26,745
  • 6
  • 48
  • 60