afxmessagebox 在APP的InitInstance()的失效?

aa65433 2007-10-25 04:32:44
今天突然发现对话框APP的InitInstance()在对对话框进行DoModal后在用AfxMessageBox后没有任何效果产生,如下: CTestDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
AfxMessageBox("test");
AfxMessageBox("test")运行后什么效果也没有,大家知道为什么么???请教一下。
...全文
193 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
小菩提的尾巴 2012-04-25
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 的回复:]

因为你要是跟踪AfxMessageBox的代码的话,就会发现它要用m_pMainWnd,而m_pMainWnd其实就是这个dlg,而模态的dialog,是自动的create和destroy的,所以一旦DoModal返回之后,这个dialog也就已经destroy了,所以这个时候就无法使用了

这个时候,你用TRACE,直接的往output窗口里面输出,并且程序运行结束之后也可以看到。
[/Quote]
我想问一下,对如下代码怎么解释呢。。。
	AfxMessageBox("Start");
CClockDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK

}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel

}
AfxMessageBox("End");
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;

也是在基于对话框的InitInstance里面,可以弹出Start,却不能弹出End
aa65433 2007-10-26
  • 打赏
  • 举报
回复
OK
有点明白了
wjkgz 2007-10-25
  • 打赏
  • 举报
回复
因为你要是跟踪AfxMessageBox的代码的话,就会发现它要用m_pMainWnd,而m_pMainWnd其实就是这个dlg,而模态的dialog,是自动的create和destroy的,所以一旦DoModal返回之后,这个dialog也就已经destroy了,所以这个时候就无法使用了

这个时候,你用TRACE,直接的往output窗口里面输出,并且程序运行结束之后也可以看到。
aa65433 2007-10-25
  • 打赏
  • 举报
回复
sdi可以用的
aa65433 2007-10-25
  • 打赏
  • 举报
回复
我就是想在DoModal(); 后弹点提示信息,做的是卸载程序,断点调试太困难的。
点了对话框的确定还没有算关闭对话框的??
aa65433 2007-10-25
  • 打赏
  • 举报
回复
期待有更明确的解答...
liuxiuk 2007-10-25
  • 打赏
  • 举报
回复
...

你的工程是基于对话框的..?

DoModal()MSDN说明:
Call this member function to invoke the modal dialog box and return the dialog-box result when done. This member function handles all interaction with the user while the dialog box is active. This is what makes the dialog box modal; that is, the user cannot interact with other windows until the dialog box is closed.

-----

这样就ok 了:

AfxMessageBox("That is ok..!");
CYourDialog dlg;
dlg.DoModal();


aa65433 2007-10-25
  • 打赏
  • 举报
回复
不到5天没法加分???!!!!
晕倒!!!
aa65433 2007-10-25
  • 打赏
  • 举报
回复
应该是的,AfxMessageBox估计也要用到m_pMainWnd...
加点分 继续讨论下大家
ppfly2008 2007-10-25
  • 打赏
  • 举报
回复
AfxMessageBox的源吗应该可以发现问题~
AfxMessageBox也是对话框

m_pMainWnd = &dlg;
dlg.DoModal(); 之后可能会对m_pMainWnd产生影响 进而影响到AfxMessageBox;

没有源吗,猜测^_^
aa65433 2007-10-25
  • 打赏
  • 举报
回复
to liuxiuk
这个我就是无辜的了,这个是向导做的...
liuxiuk 2007-10-25
  • 打赏
  • 举报
回复
InitInstance()
你return FALSE;..?


----> return TRUE;
aa65433 2007-10-25
  • 打赏
  • 举报
回复
to yxz_lp :
-----------
程序没有结束啊 ,断点执行能继续执行到return FALSE; 的啊
aa65433 2007-10-25
  • 打赏
  • 举报
回复
BOOL CTestApp::InitInstance()
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CTestDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
AfxMessageBox("test");
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}

// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
完整的代码,我也是今天才发现的,比较奇怪的啊,我开始还以为是程序不执行的了。找了半天才发现
yxz_lp 2007-10-25
  • 打赏
  • 举报
回复
int nResponse = dlg.DoModal();//进入dlg的消息循环,阻塞当前线程。
AfxMessageBox("test"); //等dlg退出后才能执行,在这里可能没机会了,dlg退出就退出程序了。
liuxiuk 2007-10-25
  • 打赏
  • 举报
回复
代码贴完整点

---

CYourDialog dlg;

dlg.DoModal();

AfxMessageBox("That is ok..!");

这会有问题..?
wjkgz 2007-10-25
  • 打赏
  • 举报
回复
我估计是在调用了DoModal之后,dlg的hWnd已经不存在了,而这个函数最终是需要hWnd的。
其实也就是要调用pMainWnd 的hWnd的,所以就看不到了。
wjkgz 2007-10-25
  • 打赏
  • 举报
回复
m_pMainWnd = &dlg;//注释掉
这个不能注释掉的,因为是Dailog的App。
aa65433 2007-10-25
  • 打赏
  • 举报
回复
m_pMainWnd = &dlg;//注释掉
可以的。不过有没有人讲解一下原因的??

---------------------------------
就是直接InitInstance()里的DoModal后面添加AfxMessageBox啊
ppfly2008 2007-10-25
  • 打赏
  • 举报
回复

m_pMainWnd = &dlg;//注释掉
加载更多回复(1)

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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