对话框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 );//会引起内存访问错误
}

...全文
363 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
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!
}

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧