c++ - 维护最近的文件列表

标签 c++ mfc recent-file-list

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

我一直在研究 Eugene Kain 的“The MFC Answer Book”中的一个示例,它可以以编程方式将字符串添加到基于标准文档/ View 架构的应用程序的“最近使用的文件”列表中:(请参阅“管理最近使用的文件列表”( MRU)"):

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

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

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

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

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

最佳答案

我最近使用 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();
}

//..

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

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

告诉我这是否适合您。不确定我是否拥有一切。

关于c++ - 维护最近的文件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1921231/

相关文章:

c++ - 计算机图形行业的人们使用什么进行光线追踪?

c++ - CScrollView 只滚动大面积的部分区域

c++ - HitTest 未按预期工作

c++ - 如何正确刷新ribbon的MFC默认最近文档列表?

xcode - mac osx最近的项目,即使我从偏好设置(如5)更改了它,也再次将其重置为无

emacs - 使用recentf 强制emacs 最近的文件忽略指定的文件(例如.windows 和.revive)

C++ CodeBlocks 反汇编;代码太多了?

c++ - X11:XQueryPointer 给我模糊的窗口

c# - 从 C++ 应用程序调用 C# .NET 服务?

c++ - QT QImage - 将图像的子部分复制为多边形