直接上代码
void CDlgSpeechDlg::OnView()
{
// TODO: Add your control notification handler code here
if(nView==0)
{
CRect rt;
GetWindowRect(&rt);//rt.right,rt.top//ScreenToClient(&rt);
::PlaySound(LPCTSTR(IDR_BEGIN),::AfxGetInstanceHandle(),SND_RESOURCE|SND_ASYNC);
CViewDlg *pDlg=new CViewDlg();
pDlg->Create(CViewDlg::IDD);
pDlg->SetWindowPos(NULL,rt.right,rt.top,0,0,SWP_NOSIZE);
pDlg->ShowWindow(SW_RESTORE);
nView=1;
}
else if(nView==1)
{
CViewDlg *pDlg;
pDlg=(CViewDlg*)AfxGetMainWnd()->GetDlgItem(IDD_VIEW);
pDlg->OnCancel();
nView=0;
}
}void CViewDlg::OnCancel()
{
DestroyWindow();
}创建新窗口的时候没问题。。关闭的时候一按就出错误内存错误。。
大家帮忙看看
void CDlgSpeechDlg::OnView()
{
// TODO: Add your control notification handler code here
if(nView==0)
{
CRect rt;
GetWindowRect(&rt);//rt.right,rt.top//ScreenToClient(&rt);
::PlaySound(LPCTSTR(IDR_BEGIN),::AfxGetInstanceHandle(),SND_RESOURCE|SND_ASYNC);
CViewDlg *pDlg=new CViewDlg();
pDlg->Create(CViewDlg::IDD);
pDlg->SetWindowPos(NULL,rt.right,rt.top,0,0,SWP_NOSIZE);
pDlg->ShowWindow(SW_RESTORE);
nView=1;
}
else if(nView==1)
{
CViewDlg *pDlg;
pDlg=(CViewDlg*)AfxGetMainWnd()->GetDlgItem(IDD_VIEW);
pDlg->OnCancel();
nView=0;
}
}void CViewDlg::OnCancel()
{
DestroyWindow();
}创建新窗口的时候没问题。。关闭的时候一按就出错误内存错误。。
大家帮忙看看
完了之后应该
delete pDlg;
pDlg指针你最好缓存起来.2. pDlg=(CViewDlg*)AfxGetMainWnd()->GetDlgItem(IDD_VIEW);
这里不一定能得到窗口IDD_VIEW的指针
{
// TODO: Add your specialized code here and/or call the base class
delete this;
CDialog::PostNcDestroy();
}
PostNcDestroy()中delete
m_pDlg=new CViewDlg();
m_pDlg->Create(CViewDlg::IDD);
m_pDlg->SetWindowPos(NULL,rt.right,rt.top,0,0,SWP_NOSIZE);
m_pDlg->ShowWindow(SW_RESTORE);
m_pDlg->OnCancel();
{
// TODO: Add your specialized code here and/or call the base class
delete this; //CDialog::PostNcDestroy();
}
大家帮帮忙啊。搞了半天都搞不懂。。它老是有这个错误
DlgSpeech.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。
大家帮我找找错误啊。。其他的都没问题,就这里
{
//DestroyWindow();
CDialog::OnCancel();
} try it.
Of course, there are some different ways to do it. But the simplest way is 全局变量.
I think, you should do it. Otherwise, your program will have a memory leak problem.
http://topic.csdn.net/t/20000419/13/7215.html
在关闭时加上这个,然后,在析构函数中别忘了释放new的内存
// CDlgSpeechDlg.h中
CViewDlg* m_pViewDlg; // 在CDlgSpeechDlg中记得先要初始化为NULLvoid CDlgSpeechDlg::OnView()
{
// TODO: Add your control notification handler code here
if(nView==0)
{
CRect rt;
GetWindowRect(&rt);//rt.right,rt.top//ScreenToClient(&rt);
::PlaySound(LPCTSTR(IDR_BEGIN),::AfxGetInstanceHandle(),SND_RESOURCE|SND_ASYNC); if ( m_pViewDlg != NULL && m_pViewDlg.GetSafeHwnd() ) // 窗口自己关闭了
{
delete m_pViewDlg;
m_pViewDlg = NULL;
}if ( m_pViewDlg == NULL )
{
m_pViewDlg = new CViewDlg();
m_pViewDlg->Create(CViewDlg::IDD);
}
m_pViewDlg->SetWindowPos(NULL,rt.right,rt.top,0,0,SWP_NOSIZE);
m_pViewDlg->ShowWindow(SW_RESTORE);
nView=1;
}
else if(nView==1)
{
if ( m_pViewDlg != NULL )
{
if ( m_pViewDlg.GetSafeHwnd() )
::DestroyWindow( m_pViewDlg.GetSafeHwnd() );
delete m_pViewDlg;
m_pViewDlg = NULL;
}nView=0;
}
} // 这个就不用了,去掉
void CViewDlg::OnCancel()
{
//DestroyWindow();
}
{if ( m_pViewDlg!= NULL )
{if ( m_pViewDlg.GetSafeHwnd() )
::DestroyWindow( m_pViewDlg.GetSafeHwnd() );
delete m_pViewDlg; //你这里多DELETE了一次。。去掉就没事了
m_pViewDlg= NULL;
} 你这里多DELETE了一次