c++ - 基于 MFC 对话框的应用程序无法调用对话框两次

标签 c++ mfc dialog

我有一个基于 MFC 对话框的应用程序,我想在其中更改对话框。为此,我关闭对话框并尝试使用另一个对话框模板再次加载它。对话框的第二次调用失败,返回代码为 -1。

即使不更改模板,问题仍然存在。 GetLastError() 返回 0。我使用 AppWizard 生成了最简单的示例。

App 向导在 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 的文档

The Microsoft Foundation Class Library will automatically terminate your thread when the window referred to by m_pMainWnd is closed. If this thread is the primary thread for an application, the application will also be terminated. If this data member is NULL, the main window for the application's CWinApp object will be used to determine when to terminate the thread. m_pMainWnd is a public variable of type 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 line\n");

    return FALSE;
}

要修复它,您可以删除行 //m_pMainWnd = pdlg; 并让 MFC 处理它。

更好的是,改变程序设计,使 GUI 线程始终有一个主窗口。

关于c++ - 基于 MFC 对话框的应用程序无法调用对话框两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728573/

相关文章:

c++ - 自定义按钮形状

c++ - 排序排列的算法

c++ - 函数中的局部变量应该如何初始化?

c++ - MFC 控件上的文本 - Unicode 字符(例如日语)被截断

c++ - Qt - Esc 不应该关闭对话框

android - 在 android 中以 12 或 24 小时格式显示 TimePicker

c++ - 如何从值中获取精确的整数

c++ - 当系统区域设置为法语时,德语资源仅从资源 dll 加载

c++ - 按钮控件需要一些时间才能在对话框中禁用

java - 模态对话框不工作