Solved

Destroy a Modeless Dialog

Posted on 2003-11-25
7
1,135 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:Dj_Fx8
  • 4
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
htang_us earned 350 total points
ID: 9820997
You can try this.

In your modeless dialog, overwrite this method PostNcDestroy() and put the following code there

void A::PostNcDestroy()
{
   CDialog::PostNcDestroy();
   if(m_pParent) // m_pParent is the pointer to parent window
        ((CMainFrame*)m_pParent->m_wndCdBurnerDlg) = NULL;
   delete this;
}

You also need to overwrite OnBnClickedOk and OnBnClickedCancel,

void A::OnBnClickedOk()
{
DestroyWindow();
}

void A::OnBnClickedCancel()
{
DestroyWindow();
}

That will delete this modeless window without waiting to the end of your application.

-Htang
0
 
LVL 2

Expert Comment

by:htang_us
ID: 9821018
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
0
 
LVL 2

Expert Comment

by:htang_us
ID: 9821049
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Dj_Fx8
ID: 9821261
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();
}
0
 
LVL 2

Expert Comment

by:htang_us
ID: 9821415
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.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9822620
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;
  }
}
0
 

Author Comment

by:Dj_Fx8
ID: 9839969
Hi
Sorry I was away a few days, It works fine htang thanks, sorry Alex htang got there just before you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now