基于 MFC 对话框的应用程序无法调用对话框两次
MFC dialog based application fails to invoke dialog twice
我有一个基于 MFC 对话框的应用程序,我想在其中更改对话框。为此,我关闭对话框并尝试使用另一个对话框模板再次加载它。对话框的第二次调用失败,返回代码为 -1。
即使不更改模板,问题也保持不变。GetLastError(( 返回 0。我使用AppWizard生成了最简单的示例。
应用向导在 CMyApp::InitInstance 中生成以下代码:
CMyAppDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
...
我改成了:
CMyAppDlg *pdlg = new CMyAppDlg;
m_pMainWnd = pdlg;
INT_PTR nResponse = pdlg->DoModal();
if (nResponse == IDOK)
{}
delete pdlg;
CMyAppDlg dlg1;
m_pMainWnd = &dlg1; // leaving this out makes no difference
nResponse = dlg1.DoModal();// this exits immediately with a -1
if (nResponse == IDOK)
。
第一个DoModal((工作正常。当我按确定或取消时,第二个 DoModal(( 失败返回 -1。
来自m_pMainWnd
的文档
Microsoft基础类库将自动终止 关闭 m_pMainWnd 引用的窗口时的线程。如果 此线程是应用程序(应用程序(的主线程 也将被终止。如果此数据成员为 NULL,则主窗口 对于应用程序的 CWinApp 对象,将用于确定何时 终止线程。m_pMainWnd 是 CWnd* 类型的公共变量。
因此,当主窗口关闭时,MFC 已经决定应用程序已结束,不会创建其他窗口。
最少的可重现代码:
BOOL CMyWinApp::InitInstance()
{
CWinApp::InitInstance();
CDialog *pdlg = new CDialog(IDD_DIALOG1);
m_pMainWnd = pdlg; //<- remove this to see the message box
pdlg->DoModal();
m_pMainWnd = NULL; //<- this line has no effect basically
delete pdlg;
MessageBox(0, L"You won't see this message box", 0, 0);
TRACE("but you will see this debug linen");
return FALSE;
}
若要修复它,可以删除行//m_pMainWnd = pdlg;
,然后让 MFC 处理它。
更好的是,更改程序设计,以便始终有一个用于 GUI 线程的主窗口。
相关文章:
- 如何在Qbutton的帮助下更改Q对话框的宽度
- g++的分段错误(在NaN上使用to_string两次时)
- 当对话框被接受时,如何用参数调用槽
- 蛇在C++不会连续转两次
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 如何多次询问来自QML对话框的输入?
- 选择文件对话框错误
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 自定义对话框(源自QDialog)不会关闭(Android)
- 基于 MFC 对话框的应用程序无法调用对话框两次
- 使两个QT对话框相互关闭
- CEdit 框的NM_KILLFOCUS被调用两次
- 如何连接两个对话框
- Qt在运行时改变对话框的模式,而不是第一次隐藏它,然后再显示
- 如何在mfc的两个对话框之间传递数据
- Win32 API从带有两个组合框的对话框中读取组合框值
- 是否有可能在Qt上的一个窗口中显示两个不同的对话框窗口