Click here to Skip to main content
Click here to Skip to main content

Closing unused MDI documents with 1 line of code

By , 1 Dec 1999
 

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)

About the Author

Warren Stevens
Software Developer (Senior)
Canada Canada
Member
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).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalthe list is no more correctsussdeliu224 Aug '02 - 1:30 
The default window is closed, just what i wanted.
But the list in the menu "window" can only list two items of chrild frames. Who can help?
GeneralVia CWinApp::InitInstance()memberSeby19 Dec '01 - 6:40 
You just have in fact to catch the "Open file" command returned by default from ParseCommandLine(...) in your InitInstance()
 
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
 
// Here is the modification : change the default command by nothing
if (cmdInfo.m_nShellCommand==CCommandLineInfo::FileNew)
cmdInfo.m_nShellCommand=CCommandLineInfo::FileNothing;
 
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
GeneralRe: Via CWinApp::InitInstance()memberWarren Stevens19 Dec '01 - 10:57 
Your suggested code gives different behaviour than the code in the article - your code stops the default document from coming up. (i.e. the user has to press "new" if they want to start working with the default document)
 
The code in the article gives a default document (if they want to start working with the default document), but it will close the default document if they open a file without modifying the default document (so they don't have to close the default document to get it out of the list of windows on the window menu) Word and Excel from Office 2000 have this behaviour.
 
Warren

GeneralRe: Via CWinApp::InitInstance()memberDuane13 Apr '02 - 16:26 
Seby.. Your suggestion was exactly what I was looking for. Thanks.
GeneralCouldn't resistmemberNorm Almond1 Sep '01 - 1:16 
PostQuitMessage(0);

Generalvery deceptive...memberyeahRight7 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...memberWarren Stevens7 Aug '01 - 8:31 
One line of code is what YOU have to add to your project. (i.e. almost zero work to add it to a project)

GeneralRe: very deceptive...memberNish [BusterBoy]28 Nov '01 - 5:57 
That's 21st century marketing for you Smile | :)
 
Nish
 
Sonork ID 100.9786 voidmain
www.busterboy.org
If you don't find me on CP, I'll be at Bob's HungOut

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 2 Dec 1999
Article Copyright 1999 by Warren Stevens
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid