Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing an MFC SDI app. I open a document in a method and call
pDoc = ((CFrameWnd*)pWndMain)->GetActiveDocument();

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?
Posted
Comments
Jackie Lloyd 3-Aug-11 3:59am    
I am pretty sure I don't understand the lifetime of the doc class. Does it live throughout th etime the app is open, or does it begin its life when the user selects to open or create a new doc, and then die when the user closes the doc?

You should never call OnCloseDocument explicitely, the framework calls it.
I'm not an MFC expert, but I guess you may call the SetWindowText of your Main Frame window.
As about document life cycle, you may find a bit of (dated) info here here[^].
 
Share this answer
 
v2
Comments
Jackie Lloyd 3-Aug-11 5:41am    
SetWindowText set the main frame title successfully, so thankyou.
I read the article but am still slightly confused. SDI apps reuse the same doc - right? So the destructor for the doc class should not be called until the app closes, not called when the user closes a file? The artcle seems to say that I should call CWinApp::OnCloseFile() when I close a file, but this calls the doc destructor and then the program crashes. Is that because it is not an MDI app?
CPallini 3-Aug-11 5:53am    
I suppose your interpretation is right, the clue is: "An MDI application creates a new CDocument class for every open document, whereas an SDI program reuses a single document." so the document dtor should be called only on application exit.
Jackie Lloyd 3-Aug-11 6:46am    
Thankyou - it's helpful to know I am understanding things right. Not sure I am totally there yet though!
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.
 
Share this answer
 
Comments
Jackie Lloyd 3-Aug-11 6:58am    
I have been setting the doc title using SetTitle, but when I closed a document and did SetTitle(_T("")) it left a hyphen in the main frame title bar. That's why I resorted to using SetWindowText on the main frame. However, from what I've read using DeleteContents sound slike what I need so I will try it and update with outcome. Thankyou very much :)
Jackie Lloyd 3-Aug-11 11:19am    
I have tried putting DeleteContents() into my method which closes the file. Although it seems to clear the doc title from the doc object, it does not update the title shown in the main frame. Should I stil use SetWindowText to clear the title shown in the main frame?
krmed 3-Aug-11 13:19pm    
Please post the code you have - maybe that will help see where your problem is.
Jackie Lloyd 4-Aug-11 4:49am    
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"));
}
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"));
}
 
Share this answer
 
Comments
krmed 4-Aug-11 6:31am    
Please show the DeleteContents method - what's being done in there? It should be resetting all doc variables to be as if you have not yet opened a file. You also don't show where you are calling the SetTitle() method of the doc.
Jackie Lloyd 4-Aug-11 7:10am    
I havn't overridden DeleteContents - so its just calling the CDocument Version. which I thought would clear the title and path. I took the call to SetTitle(_T("")) out as there didn't seem much point as I still had to set the main frame title elsewhere.
So, if in DeleteContents I delete the title and path, is there a better to get the main frame title to update?

Many thanks for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900