MultiDocTemplate, single window

I am using MultiDocTemplate. When I open a new document (child window), how can I make the program to close the old one automatically? I only need one or no child window on the screen anytime.
Don't suggest me to use SingleDocTemplate,please.
JHuangAsked:
Who is Participating?
 
naveenkohliConnect With a Mentor Commented:
In OnNewDocument function of your doc class, get the pointer to current active document. If its not NULL and is valid, then close it first and then continue on with opeing of new one.

CMyDoc::OnNewDocument ()
{
CWnd *pWnd = AfxGetMainWnd ();
CFrameWnd *pFrame = static_cast<CFrameWnd *>(pWnd);
CFrameWnd *pActiveFrameWnd = pFrame->GetActiveFrame ();

CDoucment *pDoc = pActiveFrameWnd->GetActiveDocument ();

if (pDoc != NULL) {
pDoc->SaveModified ();
pDoc->OnCloseDocument ();
}
}
0
 
JHuangAuthor Commented:
Yes, you are right.
I tested. It works fine.

You have earned 50 points already.

The following is an extera question:

How to close an old child frame when opening a new frame?

I put       
ON_COMMAND(ID_FILE_NEW, OnFileNew)
in CMainFrame,
CMainFrame::OnFileNew will creat a new frame.

When I press File & New, it will open a new window, too.
This time it's a Frame, not a Document, isn't it?

At this time, you answer won't work.

In any case, please tell me the result.


0
 
naveenkohliCommented:
The way this message works is that when you open a new file, the call has to go through OnNewDcoument. Thats why i suggested to put that close call in OnNewDocument. Try it, you should not have any probelm in closing the frame.

Good Luck!
0
 
JHuangAuthor Commented:
Thank you!
0
All Courses

From novice to tech pro — start learning today.