Solved

Destroy a Modeless Dialog

Posted on 2003-11-25
7
1,155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Title # Comments Views Activity
Folder Comparison 12 55
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 547
maxBlock challenge 30 150
either24  challenge 19 109
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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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