Solved

Destroy a Modeless Dialog

Posted on 2003-11-25
7
1,138 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

11 Experts available now in Live!

Get 1:1 Help Now