Dj_Fx8
asked on
Destroy a Modeless Dialog
Hi
In my CMainFrame I have created a modeless dialog as follows
void CMainFrame::OnButtonCdburn er()
{
// m_wndCdBurnerDlg is a member var of CMainFrame
if (m_wndCdBurnerDlg != NULL) {
m_wndCdBurnerDlg->ShowWind ow(SW_SHOW );
m_wndCdBurnerDlg->BringWin dowToTop() ;
}
else {
m_wndCdBurnerDlg = new CCdBurnerDlg(this);
BOOL ret = m_wndCdBurnerDlg->Create(I DD_DIALOG_ CDBURNER, this);
if (!ret)
AfxMessageBox("Error creating dialog");
m_wndCdBurnerDlg->ShowWind ow(SW_SHOW );
m_wndCdBurnerDlg->BringWin dowToTop() ;
}
}
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
In my CMainFrame I have created a modeless dialog as follows
void CMainFrame::OnButtonCdburn
{
// m_wndCdBurnerDlg is a member var of CMainFrame
if (m_wndCdBurnerDlg != NULL) {
m_wndCdBurnerDlg->ShowWind
m_wndCdBurnerDlg->BringWin
}
else {
m_wndCdBurnerDlg = new CCdBurnerDlg(this);
BOOL ret = m_wndCdBurnerDlg->Create(I
if (!ret)
AfxMessageBox("Error creating dialog");
m_wndCdBurnerDlg->ShowWind
m_wndCdBurnerDlg->BringWin
}
}
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
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Hi htang
Itried out your suggestion and it works fine only I changed the code in PostNCDestroy as follows
void CCdBurnerDlg::PostNcDestro y()
{
CMainFrame* pMainFrm = (CMainFrame*)AfxGetMainWnd ();
pMainFrm->m_wndCdBurnerDlg = NULL;
delete this ;
CDialog::PostNcDestroy();
}
Itried out your suggestion and it works fine only I changed the code in PostNCDestroy as follows
void CCdBurnerDlg::PostNcDestro
{
CMainFrame* pMainFrm = (CMainFrame*)AfxGetMainWnd
pMainFrm->m_wndCdBurnerDlg
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.
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_wnd CdBurnerDl g->m_hWnd) )
{
pMainFrm->m_wndCdBurnerDlg ->DestroyW indow();
}
delete pMainFrm->m_wndCdBurnerDlg ;
pMainFrm->m_wndCdBurnerDlg = NULL;
}
}
{
CMainFrame* pMainFrm = (CMainFrame*)AfxGetMainWnd
if ( pMainFrm->m_wndCdBurnerDlg
{
if ( ::IsWindow(pMainFrm->m_wnd
{
pMainFrm->m_wndCdBurnerDlg
}
delete pMainFrm->m_wndCdBurnerDlg
pMainFrm->m_wndCdBurnerDlg
}
}
ASKER
Hi
Sorry I was away a few days, It works fine htang thanks, sorry Alex htang got there just before you
Sorry I was away a few days, It works fine htang thanks, sorry Alex htang got there just before you
-htang