【发布时间】:2019-08-30 14:24:06
【问题描述】:
我有一个基于 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() 工作正常。当我按 OK 或 Cancel 时,第二个 DoModal() 无法返回 -1。
【问题讨论】:
-
"对话框的第二次调用失败,返回码为 -1。" 在看到这样的返回值后,您是否尝试查看
GetLastError返回的内容,如在documentation?中描述? -
@alg:您不能在 MFC 应用程序中调用
GetLastError。请再次阅读文档。 -
@IInspectable 请提供报价,说明不能在 MFC 应用程序中调用
GetLastError。 -
如果
DoModal返回IDABORT,CDialog::DoModal的文档说GetLastError会提供更多信息。否则GetLastError没有用处。在这种情况下GetLastError为 0,因为没有 Windows 错误,这是一个 MFC 问题。 -
@bar:文档没有这么说。