MFC: how to clear recent file list of SDI app?

Viewed 245

Is there a way to clear the recent file list under the File menu? I try to do something with "m_pRecentFileList" but so far no success yet with error C2027: use of undefined type 'CRecentFileList'.

BOOL CMRCApp::InitInstance()
{
    ...

    while (m_pRecentFileList->GetSize() > 0)
        m_pRecentFileList->Remove(0);  // no Clear function ??

    return TRUE;
}

Edit

After fixing the compilation error, the while loop becomes infinite. The Remove(...) function doesn't seem to decrement the list.

1 Answers

Your code has the right idea! However, to access the m_pRecentFileList member's functions, you have to include the afxadv.h header file.

EDIT: As the Remove(0) call doesn't seem to alter the value returned by GetSize() (as you have noted in the comments), you should keep track of the list's size yourself.

Something like this would do the trick:

#include <afxadv.h> // For the definition of the CRecentFileList class
//...
void CMRCApp::ClearRFL()
{
    int nRFL = m_pRecentFileList->GetSize();
    while (nRFL > 0) m_pRecentFileList->Remove(--nRFL);
    return;
}

Also, be sure to put the call to this function (or it's code) at (or very near) the end of your InitInstance override (and certainly after the base class InitInstance has been called) so that the framework has actually finished loading the MRU. You should also make sure it comes after any explicit call to LoadStdProfileSettings().

Related