维护最近的文件列表

And*_*yUK 6 c++ mfc recent-file-list

我想在我的MFC应用程序上维护一个简单的最近文件列表,其中显示了最近使用的4个文件名.

我一直在玩Eugene Kain的"The MFC Answer Book"中的一个例子,该例子可以编程方式将字符串添加到基于标准Document/View架构的应用程序的Recent Files列表中:(参见"管理最近的文件列表(M​​RU)") ):

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

我的应用程序是一个相当轻量级的实用程序,它不使用Document/View体系结构来管理数据,文件格式等.我不确定上面例子中使用的相同原则是否适用于此.

有没有人有任何关于如何维护文件菜单中显示的最近文件列表的例子,并且可以存储在某个文件/注册表设置中?最重要的是,我缺乏知识和理解阻碍了我.

更新:我最近发现这个CodeProject文章非常有用......

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

Tho*_*ini 5

您可以使用boost循环缓冲区算法在程序运行时维护列表,然后在每次更新时将其保存到注册表(应该是微不足道的),并在程序首次启动时加载它。

  • 因为boost的循环缓冲区在空间不足时会自动擦除插入的第一个元素。它是专门为诸如最近的文件列表之类的东西而设计的。对于有 4 个位置的向量,您必须识别最旧的元素,如果已满则将其删除;有了 boost,一切都会自动完成! (2认同)

n1c*_*ckp 5

我最近使用MFC进行了此操作,因此既然您似乎也正在使用MFC,也许它会有所帮助:

在:

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();
}
Run Code Online (Sandbox Code Playgroud)

// ..

//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]);
}
Run Code Online (Sandbox Code Playgroud)

// ...

//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;
    }
}
Run Code Online (Sandbox Code Playgroud)

您只需要从CWinApp派生您的应用程序即可(我使用从CFrmWnd派生的类来处理菜单,也许您也这样做吗?)。

告诉我这是否适合您。不知道我是否拥有一切。