MFC DoModal() 调用后立即返回 MessageBox
该主题说明了一切:我遇到一个问题,如果在 CDialog::DoModal() 之后调用 MessageBox,它会立即返回(不显示任何内容)。我尝试过各种 MessageBox:MessageBoxA、MessageBoxW、CWnd::MessageBox(通过使用对话框)、AfxMessageBox。没有一个有效,并且它们立即返回 1。我尝试保存对话框的 HWND 并将其用作第一个参数。我尝试将 GetDesktopWindow() 作为 HWND 参数传递。我尝试了诸如 MB_YESNO|MB_ICONSTOP 之类的东西。什么都没起作用。
我怀疑我错过了一些非常明显的东西,但对于我的一生,我无法弄清楚什么,而且我已经寻找解决方案两个多小时了。我尝试使用 MFC 向导创建一个新项目,选择对话框模板,然后在 DoModal() 调用之后立即添加对 MessageBoxA 的调用:
CdelmeDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
MessageBoxA(0, "test", "test", 0);
对话框显示得很好,但是当我单击“确定”或“取消”时,消息框只是显示不出现。
如果我将 MessageBoxA() 调用放在 DoModal() 调用之上,则它可以正常工作。
编辑:有什么办法可以防止这种行为吗?
The topic says it all: I'm having a problem where MessageBox returns immediately (without displaying anything) if it is being called after a CDialog::DoModal(). I've tried all kinds of MessageBox: MessageBoxA, MessageBoxW, CWnd::MessageBox (by using the dialog), AfxMessageBox. None works and they return 1 immediately. I tried saving the HWND of the dialog and using that as the first parameter. I tried passing GetDesktopWindow() as the HWND parameter. I tried stuff such as MB_YESNO|MB_ICONSTOP. Nothing worked.
I suspect I'm missing something really obvious but for the life of me, I can't figure out what and I've been looking for a solution for over 2 hours now. I tried by creating a new project using the MFC Wizard, selecting the dialog template and simply adding a call to MessageBoxA right after the DoModal() call:
CdelmeDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
MessageBoxA(0, "test", "test", 0);
The dialog displays just fine, but when I click Ok or Cancel, the message box simply does not show up.
If I place the MessageBoxA() call above the DoModal() call, it works perfectly.
Edit: is there any way to prevent this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据记忆:
MFC 框架的设计方式是,如果主窗口(在您的情况下为 dlg)退出,消息循环就会结束,并且您将不再可以通过 GUI 执行任何操作。
更多信息(来自创作者):
来自:
http://msdn.microsoft.com/ en-us/library/f3ddxzww(v=vs.80).aspx
From memory:
MFC framework is designed in a way that if main window (in your case
dlg
) exits, message loops is over and there will be no more of anything that you can do GUI-wise.More info (from the Creators):
from:
http://msdn.microsoft.com/en-us/library/f3ddxzww(v=vs.80).aspx