对话框DoModal后,我在其它函数中想结束这个对话框调((CT01Dlg*)theApp.m_pMainWnd)->EndDialog( 0L )出错,应该怎么改?

ming6 2005-09-29 05:06:04
函数一
{
CMyDlg dlg;
theApp.m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
}
我想在函数2中结束函数1中产生的对话框
函数二
{
((CMyDlg*)theApp.m_pMainWnd)->EndDialog( 0L );//会引起内存访问错误
}

...全文
276 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
gwinner 2005-09-30
内存访问错误是什么?很可能是另外的原因引起的,对Modal窗口用EndDialog没错,查一下内存的问题。

你是不是在析构中释放内存了?或的构造中初始化内存?
回复
ming6 2005-09-30
to gunney(楚州才子)
EndDialog在DoModal返回后才用

那应该怎么在DoModal没有返回时,在外部安全的关闭
我试了
theApp.m_pMainWnd->PostMessage(WM_CLOSE);
发现不行
回复
ming6 2005-09-30
发送消息:
theApp.m_pMainWnd->PostMessage(WM_CLOSE);
---------------------------------
我试试看
回复
ming6 2005-09-30
一直没有搞清哪有问题,
我这个
modal dialog
是写在一个服务程序中的
开始服务时就DoModal
DoModal在没有返回时是阻塞的

当结束服务就需要关闭这个对话框

关闭时我用EndDialog就会出错
回复
ming6 2005-09-30
调试发现中断在

void CWinApp::EnableModeless(BOOL bEnable)
{
#ifdef _AFX_NO_OLE_SUPPORT
UNUSED(bEnable);
#endif

// no-op if main window is NULL or not a CFrameWnd
CWnd* pMainWnd = AfxGetMainWnd();
if (pMainWnd == NULL || !pMainWnd->IsFrameWnd())//中断在这里!!!
return;

#ifndef _AFX_NO_OLE_SUPPORT
// check if notify hook installed
ASSERT_KINDOF(CFrameWnd, pMainWnd);
CFrameWnd* pFrameWnd = (CFrameWnd*)pMainWnd;
if (pFrameWnd->m_pNotifyHook != NULL)
pFrameWnd->m_pNotifyHook->OnEnableModeless(bEnable);
#endif
}
回复
flyelf 2005-09-29
发送消息:
theApp.m_pMainWnd->PostMessage(WM_CLOSE);
回复
gunney 2005-09-29
MSdN 上这样说
Remarks
This member function returns nResult as the return value of DoModal. You must use the EndDialog function to complete processing whenever a modal dialog box is created.

You can call EndDialog at any time, even in OnInitDialog, in which case you should close the dialog box before it is shown or before the input focus is set.

EndDialog does not close the dialog box immediately. Instead, it sets a flag that directs the dialog box to close as soon as the current message handler returns.

Example
/* MyWnd.cpp */
#include "MyDialog.h"

void CMyWnd::ShowDialog()
{
CMyDialog myDlg;
int nRet = myDlg.DoModal();

if ( nRet == IDOK || nRet == 5 )
AfxMessageBox("Dialog closed successfully");
}

/* MyDialog.cpp */
void CMyDialog::OnSomeAction()
{
// Do something

int nRet = 5; // Just any value would do!
EndDialog(nRet); // This value is returned by DoModal!

// Do something

return; // Dialog closed and DoModal returns only here!
}
回复
发帖
界面
创建于2007-09-28

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2005-09-29 05:06
社区公告
暂无公告