Adding Most Recently Used(MRU)FilestoanSDI/MDI Application

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);
    }
}

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 항목은 *(으)로 표시합니다