Link to home
Start Free TrialLog in
Avatar of Dj_Fx8
Dj_Fx8

asked on

Destroy a Modeless Dialog

Hi
In my CMainFrame I have created a modeless dialog as follows

void CMainFrame::OnButtonCdburner()
{
// m_wndCdBurnerDlg  is a member var of CMainFrame
if (m_wndCdBurnerDlg != NULL)      {
  m_wndCdBurnerDlg->ShowWindow(SW_SHOW);
  m_wndCdBurnerDlg->BringWindowToTop();
  }
else      {
  m_wndCdBurnerDlg = new CCdBurnerDlg(this);
  BOOL ret = m_wndCdBurnerDlg->Create(IDD_DIALOG_CDBURNER, this);      
  if (!ret)
    AfxMessageBox("Error creating dialog");
  m_wndCdBurnerDlg->ShowWindow(SW_SHOW);
  m_wndCdBurnerDlg->BringWindowToTop();
  }
}

I have set m_wndCdBurnerDlg  to NULL in the CMainFrame constructor and delete it in the destructor

Now this means that once the dlg is created, it is not destroyed till CMainFrame destructor is called, but I want to  destroy the dlg when I'm finished with it and not have to wait till I close my app so I put the following in my dlg

void CCdBurnerDlg::OnButton1()
{
  CMainFrame* pMainFrm = (CMainFrame*)AfxGetMainWnd();
  delete pMainFrm->m_wndCdBurnerDlg ;
  pMainFrm->m_wndCdBurnerDlg = NULL;
}

which works but I get the following msg in the debug window

Warning: calling DestroyWindow in CDialog::~CDialog --
      OnDestroy or PostNcDestroy in derived class will not be called.

I assume I'm not doing this the correct way because of this, can someone tell me where I have went wrong
ASKER CERTIFIED SOLUTION
Avatar of htang_us
htang_us

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of htang_us
htang_us

If you need to get result after user clicks OK button, you should do this before calling DestroyWindow() in OnBnClickedOk(). In my last post, I use A means CCdBurnerDlg

-htang
There is a mistake in my first post.

I said you need to overwrite OnBnClickedOk and OnBnClickedCancel, that is wrong. Should be handling BN_CLICKED message.

It can be easily created using Add Event Handler wizard if you right-click on Ok or Cancel button.

-htang
Avatar of Dj_Fx8

ASKER

Hi htang

Itried out your suggestion and it works fine only I changed the code in PostNCDestroy as follows

void CCdBurnerDlg::PostNcDestroy()
{
CMainFrame* pMainFrm = (CMainFrame*)AfxGetMainWnd();
pMainFrm->m_wndCdBurnerDlg = NULL;
delete this ;

CDialog::PostNcDestroy();
}
So If CDialog::PostNcDestory() is called first, it does not work. Is that what you mean?
If works fine in my solution. I think both should be ok.
void CCdBurnerDlg::OnButton1()
{
 CMainFrame* pMainFrm = (CMainFrame*)AfxGetMainWnd();

  if ( pMainFrm->m_wndCdBurnerDlg )
  {
      if ( ::IsWindow(pMainFrm->m_wndCdBurnerDlg->m_hWnd) )
      {
          pMainFrm->m_wndCdBurnerDlg->DestroyWindow();
      }
     delete pMainFrm->m_wndCdBurnerDlg ;
     pMainFrm->m_wndCdBurnerDlg = NULL;
  }
}
Avatar of Dj_Fx8

ASKER

Hi
Sorry I was away a few days, It works fine htang thanks, sorry Alex htang got there just before you