Click here to Skip to main content
Click here to Skip to main content
Go to top

Closing unused MDI documents with 1 line of code

, 1 Dec 1999
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Warren Stevens
Software Developer (Senior)
Canada Canada
www.IconsReview.com[^]
Huge list of stock icon collections (both free and commercial)
 
The picture is from September 2006, after picking up a rental car at the airport in Denver, Colorado. I'm smiling in the picture, because I have yet to come to the realization that I just wasted 400 bucks ( because you don't really need a car in downtown Denver - you can just walk everywhere).

Comments and Discussions

 
QuestionWorks Fine... Pinmemberfwcetus16-Jul-13 9:04 
Generalthe list is no more correct Pinsussdeliu224-Aug-02 1:30 
GeneralRe: the list is no more correct Pinmemberfwcetus16-Jul-13 9:12 
GeneralVia CWinApp::InitInstance() PinmemberSeby19-Dec-01 6:40 
GeneralRe: Via CWinApp::InitInstance() PinmemberWarren Stevens19-Dec-01 10:57 
GeneralRe: Via CWinApp::InitInstance() PinmemberDuane13-Apr-02 16:26 
GeneralCouldn't resist PinmemberNorm Almond1-Sep-01 1:16 
Generalvery deceptive... PinmemberyeahRight7-Aug-01 8:20 
one line of code... except for the 20 lines of code that are in the .h file...
 
outta here
GeneralRe: very deceptive... PinmemberWarren Stevens7-Aug-01 8:31 
GeneralRe: very deceptive... PinmemberNish [BusterBoy]28-Nov-01 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 2 Dec 1999
Article Copyright 1999 by Warren Stevens
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid