Solved

Destroy a Modeless Dialog

Posted on 2003-11-25
7
1,133 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

22 Experts available now in Live!

Get 1:1 Help Now