【发布时间】:2015-04-17 10:59:04
【问题描述】:
正如问题所说,我试图在 MFC MDI 应用程序中一次只允许一个文档。由于许多(不相关的)原因,我没有使用 SDI 文档模板。许多地方评论这样做,但我不知道如何。最接近的是此链接中的建议:http://computer-programming-forum.com/82-mfc/06d5cebffaeefeae.htm 但它不适用于 CWinAppEx - 文档已关闭,即使用户取消了“文件打开”对话框。此外,使用 MRU 列表或工具栏按钮会绕过此建议。
非常感谢任何帮助!
BOOL CMyDoc::closeDocument()
{
if (!SaveModified())
{
// User has vetoed the close, return error
return TRUE;
}
else
{
// OK to close
OnCloseDocument();
return FALSE;
}
}
在 CMyApp 中:
void CMyApp::OnFileOpen()
{
CMyDoc* pDoc = CMyDoc::GetDoc();
if (pDoc != NULL && pDoc->closeDocument())
// user has vetoed the close - can't create new one
return;
// no document currently open, or we succesfully closed it
CWinAppEx::OnFileOpen();
}
void CMyApp::OnFileNew()
{
CMyDoc* pDoc = CVATDoc::GetDoc();
if (pDoc != NULL && pDoc->closeDocument())
// user has vetoed the close - can't create new one
return;
// no document currently open, or we succesfully closed it
CWinAppEx::OnFileOpen();
}
虽然这可能在旧版本的 MFC 中有效,但现在似乎不起作用(VS2013)。在用户选择(或取消)新文档之前关闭文档。
【问题讨论】:
-
您能解释一下为什么 SDI 不适合您的“许多原因”吗?另外:请在向导生成的应用程序中显示您所做的更改。
-
我基于一个文档显示了许多视图。这些是动态创建的,并且在给定 MDI 设置的情况下,与 CDockablePanes 等一起更容易管理。代码根据链接 - 我将编辑问题。