Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Closing unused MDI documents with 1 line of code

0.00/5 (No votes)
1 Dec 1999 1  
This code closes the default "blank" document when an existing file is opened in a MFC MDI application.

This code closes the default "blank" document when an existing file is opened in a MFC MDI application (This behaviour can also be seen in Microsoft's Word and Excel).

It works for files opened from File->Open... menu option and files opened from the MRU (most-recently-used) list.

Best of all, it only requires adding one line of code to your application:

#include "CloseUnusedDocs.h"  // STEP #1: INCLUDE HEADER FILE

//

//

//

BOOL CYourDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
  if (!CDocument::OnOpenDocument(lpszPathName))
    return FALSE;        
  
  CCloseUnusedDocs::close_unused_documents(this); // STEP #2: INSERT THIS LINE

    
  return TRUE;
} 

The algorithm for the code is : loop through all of the documents (via all of the document-templates) and close the ones that match the criteria of:

  1. NOT previously saved ( CDocument::GetPathName() is empty), AND
  2. NOT Modified ( CDocument::IsModified() is zero ) AND
  3. NOT the document we are in the process of opening (the "this" pointer in the code above)

For more information you can look at the code (it's quite short, and fairly well commented).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here