维护最近的文件列表

发布于 2024-08-14 16:10:22 字数 643 浏览 7 评论 0原文

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

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

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

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

有谁有任何示例说明如何维护“文件”菜单中显示的最近文件列表,并且可以存储在某处的文件/注册表设置中?最重要的是,我缺乏知识和理解力阻碍了我。

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

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

I would like to maintain a simple recent files list on my MFC application that shows the 4 most recently used file names.

I have been playing with an example from Eugene Kain's "The MFC Answer Book" that can programmatically add strings to the Recent Files list for an application based on the standard Document/View architecture: (see "Managing the Recent Files List (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 and so on. I am not sure if the same principles used in the above example would be applicable here.

Does anyone have any examples of how they go about maintaining a recent files list that is displayed in the File menu, and can be stored in a file / registry setting somewhere? More than anything, it's my lack of knowledge and understanding that is holding me back.

Update: I have recently found this CodeProject article to be quite useful...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-08-21 16:10:23

我最近使用 MFC 做到了这一点,所以既然您似乎也在使用 MFC,也许它会有所帮助:

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

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

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

I recently did that using MFC, so since you seems to be using MFC as well maybe it 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 that your application is derived from CWinApp (and I use a class derived from CFrameWnd to handle the menu, maybe you do the same?).

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

假装爱人 2024-08-21 16:10:23

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

You can use the boost circular buffer algorithm to maintain the list while the program is running, and then save it to the registry (should be trivial) every time it's updated, and load it when the program first starts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文