关闭MFC SDI文档会导致崩溃 [英] Closing MFC SDI document causes crash
问题描述
我正在编写MFC SDI应用程序.我使用方法打开文档并调用
I am writing an MFC SDI app. I open a document in a method and call
pDoc = ((CFrameWnd*)pWndMain)->GetActiveDocument();
我使用CFileDlg选择文件,然后执行
I use a CFileDlg to select the file and then do
pDoc->SetFileName()
似乎用打开的文档标题更新了主机.
当我关闭文档时,标题不会从主框架中删除.因此,在关闭文档的方法中,我尝试调用
which seems to update the main frame with the open document''s title.
When I close the doc the title is not removed from the main frame. So, in the method that closes the doc, I have tried calling
pDoc->OnCloseDocument
但这会导致崩溃-这是因为在打开文档时我没有在其他地方正确执行操作吗?
我应该调用此命令,还是应该做
but this causes a crash - is this because I havn''t done something correctly elsewhere when the doc was opened?
Should I call this, or should I just do
pDoc->SetFileName(_T(""));,
或其他操作,以从主框架中删除文档标题?
, or something else, to remove the doc title from the main frame?
推荐答案
您永远不应调用OnCloseDocument
明确地,框架将其调用.
我不是MFC
专家,但是我想您可以调用Main Frame
窗口的SetWindowText
.
关于文档生命周期,您可以在此处 a> [ ^ ]
You should never callOnCloseDocument
explicitely, the framework calls it.
I''m not anMFC
expert, but I guess you may call theSetWindowText
of yourMain Frame
window.
As about document life cycle, you may find a bit of (dated) info here here[^].
由于这是一个SDI,因此该文档在应用程序的生命周期内一直存在.
在SDI应用程序中,在重用文档之前(例如,当您关闭"文件或打开"新文件时),框架将调用文档类的DeleteContents函数.在此功能中,您应该执行所有清理操作,以使文档显得新".
同样,使用SDI或MDI应用程序,而不是通过调用SetWindowText来设置窗口文本,而是可以让框架通过仅设置当前打开的文档的标题来处理它.为此,只需调用文档的SetTitle方法.
希望对您有所帮助.
Since this is an SDI, the document exists for the life of the application.
In an SDI app, before reusing a document (such as when you "close" the file or "open" a new one) the framework will call the document class''s DeleteContents function. It''s in this function you should do any clean up you have to do to make the document appear to be "new".
Also, with an SDI or MDI app, instead of setting the window text by calling SetWindowText, you can let the framework handle it by just setting the title of the currently open document. To do this just call the document''s SetTitle method.
Hope this helps.
这是我的代码.我的应用程序以树状视图显示文档内容.有没有一种更好的方法可以使主框架标题或树视图为空?
This is my code. My app dispalys the doc contents in a tree view. Is there a better way to get the main frame title or tree view to empty?
CMyDoc::OnFileClose()
{
DeleteContents();
m_docOpenFlag = false;
UpdateAllViews(NULL);
}
void CMyView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
//if the doc is closed
if(pMyDoc->m_docOpenFlag == false)
{
//empty the tree view
CTreeCtrl& tree = GetTreeCtrl();
tree.DeleteAllItems();
//set the main frame title
AfxGetMainWnd()->SetWindowText(_T("BWE"));
}
这篇关于关闭MFC SDI文档会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!