有两个dialog1,dialog2,从dialog1中打开dialog2,然后从dialog2中打开dialog1,并销毁dialog2. 实现如下:
dialog1.cppvoid Button_oK()
{
Cdialog2 dlg2;
dlg2.DoModel;
}dialog2.cppvoid Button_Back()
{
Cdialog1 dlg1;
dlg1.DoModel;
Cdialog2 dlg2;
dlg2.DestroyWindow();
}
执行时,dialog1打开2没问题,2返回1没问题,当1再次试图打开2时,程序中断。
中断信息:
BOOL AFXAPI AfxOleInit()
{
_AFX_THREAD_STATE* pState = AfxGetThreadState();
ASSERT(!pState->m_bNeedTerm); // calling it twice? // Special case DLL context to assume that the calling app initializes OLE.
// For DLLs where this is not the case, those DLLs will need to initialize
// OLE for themselves via OleInitialize. This is done since MFC cannot provide
// automatic uninitialize for DLLs because it is not valid to shutdown OLE
// during a DLL_PROCESS_DETACH.
if (afxContextIsDLL)
{
pState->m_bNeedTerm = -1; // -1 is a special flag
return TRUE;
}
}
dialog1.cppvoid Button_oK()
{
Cdialog2 dlg2;
dlg2.DoModel;
}dialog2.cppvoid Button_Back()
{
Cdialog1 dlg1;
dlg1.DoModel;
Cdialog2 dlg2;
dlg2.DestroyWindow();
}
执行时,dialog1打开2没问题,2返回1没问题,当1再次试图打开2时,程序中断。
中断信息:
BOOL AFXAPI AfxOleInit()
{
_AFX_THREAD_STATE* pState = AfxGetThreadState();
ASSERT(!pState->m_bNeedTerm); // calling it twice? // Special case DLL context to assume that the calling app initializes OLE.
// For DLLs where this is not the case, those DLLs will need to initialize
// OLE for themselves via OleInitialize. This is done since MFC cannot provide
// automatic uninitialize for DLLs because it is not valid to shutdown OLE
// during a DLL_PROCESS_DETACH.
if (afxContextIsDLL)
{
pState->m_bNeedTerm = -1; // -1 is a special flag
return TRUE;
}
}
这样可以,不过我按了26后程序退出,不知为什么
dialog1.cppvoid Button_oK()
{
Cdialog2 dlg2;
dlg2.DoModel;
Cdialog1 dlg1;
dlg1.DestoryWindow();
}dialog2.cppvoid Button_Back()
{
Cdialog1 dlg1;
dlg1.DoModel;
Cdialog2 dlg2;
dlg2.DestroyWindow();
}
{
Cdialog2 dlg2;
dlg2.DoModel;
DestoryWindow();
}dialog2.cppvoid Button_Back()
{
Cdialog1 dlg1;
dlg1.DoModel;
DestroyWindow();
}应该这样吧
BOOL CSecondView::OnInitDialog()
{
CDialog::OnInitDialog();
m_Ok.EnableWindow(FALSE);
return TRUE;
}
在dialog2.cppvoid Button_Back()
{
Cdialog1 dlg1;
dlg1.DoModel;
DestroyWindow();
}中应该是dlg1关闭才执行DestroyWindow();吧
那又怎么会退出呢……
我的dialog2原本是没有这个函数的。是我发现了这个错误后加上的,但错误依旧相同。
void CTest1Dlg::OnOK() {
// TODO: Add extra validation here
CDialog::OnOK();
f_add = tem;
CSecondView dlg2;
dlg2.DoModal();
// DestroyWindow();}在dialog2中执行:
void CSecondView::OnButton1() {
// TODO: Add your control notification handler code here
CDialog::OnOK();
isfirst = 1;
CTest1Dlg dlg3;
dlg3.DoModal();
CDialog::OnOK();
//DestroyWindow();
}
也可正常打开dialog1。可再执行一次CTest1Dlg::OnOK(),就报错了,无论用destroyWindow(),CloseWindow(),CDialog::OnOK()错误都一样。
BOOL CSecondView::OnInitDialog()
{
if (isfirst == 0)
{
CDialog::OnInitDialog();
m_Ok.EnableWindow(FALSE);
}
return TRUE;
}