Click here to Skip to main content
11,480,626 members (62,385 online)
Click here to Skip to main content

Adding Most Recently Used (MRU) Files to an SDI/MDI Application

, 26 Mar 2003 60.8K 1.6K 21
Rate this:
Please Sign up or sign in to vote.
Adding Most Recently Used (MRU) files to an SDI/MDI application tutorial.

Sample Image - most_recent_used.gif

Introduction

The Most Recently Used (MRU) files list is standard feature of most Windows applications. This article describes how to add (MRU) support to Windows (SDI/MDI) application using the class CRecentFileList. CRecentFileList is a CObject class that supports control of the most recently used (MRU) file list. Files can be added to or deleted from the MRU file list, the file list can be read from or written to the registry or an .INI file, and the menu displaying the MRU file list can be updated.

Using the code

Adding MRU to MFC SDI or MDI is actually not very difficult. I just add AddToRecentFileList(LPCTSTR lpszPathName) to CDocument derived class which calls the add the path name to CWinApp's CRecentFileList (m_pRecentFileList). I use SDI for this demo.

1. Iinclude afxadv.h to stdafx.h. This contains the class CRecentFileList.

#include <afxadv.h>

2. Add AddToRecentFileList to CDocument derived class and use this function during opening and saving the document.

void CCMRUTestDoc::AddToRecentFileList(LPCTSTR lpszPathName)
{
    ((CCMRUTestApp*)AfxGetApp())->AddToRecentFileList(lpszPathName);
}

BOOL CCMRUTestDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
    if (!CDocument::OnOpenDocument(lpszPathName))
        return FALSE;
    
    // Add to MRU file list
    AddToRecentFileList(lpszPathName);
    
    return TRUE;
}

BOOL CCMRUTestDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
    // Add to MRU file list
    AddToRecentFileList(lpszPathName);
    
    return CDocument::OnSaveDocument(lpszPathName);
}

3 Add AddToRecentFileList for CWinApp derived class.

void CCMRUTestApp::AddToRecentFileList(LPCTSTR lpszPathName)
{
    // lpszPathName will be added to the top of the MRU list. 
    // If lpszPathName already exists in the MRU list, it will be moved to the top
    if (m_pRecentFileList != NULL)    {
        m_pRecentFileList->Add(lpszPathName);
    }
}

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

Share

About the Author

Ramil C. Matira

Singapore Singapore
No Biography provided

Comments and Discussions

 
GeneralNice Tool Pin
valdo_gmail9-Oct-06 22:51
membervaldo_gmail9-Oct-06 22:51 
General[Message Removed] Pin
stonber1-Oct-08 8:52
memberstonber1-Oct-08 8:52 
GeneralNo action when clicking Pin
Anonymous9-Dec-04 17:05
sussAnonymous9-Dec-04 17:05 
GeneralRe: No action when clicking Pin
Ramil C. Matira11-Dec-04 0:02
memberRamil C. Matira11-Dec-04 0:02 
Generalelaborate Pin
aslamshikoh25-Jul-04 22:56
memberaslamshikoh25-Jul-04 22:56 
GeneralRe: elaborate Pin
Ramil C. Matira27-Jul-04 2:06
memberRamil C. Matira27-Jul-04 2:06 
Generalthank,it's very useful for me. Pin
ZhuoGuangSheng7-Jul-03 20:10
memberZhuoGuangSheng7-Jul-03 20:10 
GeneralHmmm Pin
Roger Allen28-Mar-03 1:11
memberRoger Allen28-Mar-03 1:11 
GeneralRe: Hmmm Pin
Habeeballah Hasnoddin15-Feb-10 10:00
memberHabeeballah Hasnoddin15-Feb-10 10:00 

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 | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 27 Mar 2003
Article Copyright 2003 by Ramil C. Matira
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid