Maintain a list of recent files

I would like to keep a simple list of recent files in my MFC application, which shows the 4 most recently used file names.

I played with an example from Eugene Kain's “MFC Answer Book”, which can programmatically add lines to the list of recent files for an application based on the standard Document / View architecture: (see “Managing the Recent File List (see MRU)”):

http://www.nerdbooks.com/isbn/0201185377

My application is a fairly lightweight utility that does not use the Document / View architecture to manage data, file formats, etc. I'm not sure that the same principles will apply here as in the above example.

Does anyone have examples of how they lead by maintaining a list of recent files that appears in the File menu and can be stored in a file / registry somewhere? More than anything, this is the lack of knowledge and understanding that holds me back.

Update: I recently found this CodeProject article very useful ...

http://www.codeproject.com/KB/dialog/rfldlg.aspx

+5
source share
2 answers

I recently did this with MFC, since you seem to be using MFC, maybe this will help:

in

BOOL MyApp::InitInstance()
{
    // Call this member function from within the InitInstance member function to 
    // enable and load the list of most recently used (MRU) files and last preview 
    // state.
    SetRegistryKey("MyApp"); //I think this caused problem with Vista and up if it wasn't there
                                 //, not really sure now since I didn't wrote a comment at the time
    LoadStdProfileSettings();
}

// ..

//function called when you save or load a file
void MyApp::addToRecentFileList(boost::filesystem::path const& path)
{
    //use file_string to have your path in windows native format (\ instead of /)
    //or it won't work in the MFC version in vs2010 (error in CRecentFileList::Add at
    //hr = afxGlobalData.ShellCreateItemFromParsingName)
    AddToRecentFileList(path.file_string().c_str());
}

//function called when the user click on a recent file in the menu
boost::filesystem::path MyApp::getRecentFile(int index) const
{
    return std::string((*m_pRecentFileList)[index]);
}

// ...

//handler for the menu
BOOL MyFrame::OnCommand(WPARAM wParam, LPARAM lParam)
{
    BOOL answ = TRUE;

    if(wParam >= ID_FILE_MRU_FILE1 && wParam <= ID_FILE_MRU_FILE16)
    {
        int nIndex = wParam - ID_FILE_MRU_FILE1;

        boost::filesystem::path path = getApp()->getRecentFile(nIndex);
        //do something with the recent file, probably load it

        return answ;
    }
}

You only need your application to be obtained from CWinApp (and I'm using a class derived from CFrmWnd to handle the menu, maybe you are doing the same?).

Tell me if this works for you. Not sure if I have everything.

+4

boost round buffer , ( ) , , .

+4

All Articles