65.9K
CodeProject 正在变化。 阅读更多。
Home

将最近使用文件 (MRU) 添加到 SDI/MDI 应用程序

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.65/5 (9投票s)

2003年3月27日

viewsIcon

80744

downloadIcon

1900

关于将最近使用文件 (MRU) 添加到 SDI/MDI 应用程序的教程。

Sample Image - most_recent_used.gif

引言

最近使用文件 (MRU) 列表是大多数 Windows 应用程序的标准功能。本文介绍了如何使用类 CRecentFileList 向 Windows (SDI/MDI) 应用程序添加 (MRU) 支持。CRecentFileList 是一个 CObject 类,支持控制最近使用文件列表。可以从 MRU 文件列表中添加或删除文件,可以将文件列表读写到注册表或 .INI 文件,并且可以更新显示 MRU 文件列表的菜单。

使用代码

将 MRU 添加到 MFC SDI 或 MDI 实际上并不难。我只需将 AddToRecentFileList(LPCTSTR lpszPathName) 添加到 CDocument 派生类中,该类调用将路径名添加到 CWinAppCRecentFileList (m_pRecentFileList)。我在此演示中使用 SDI。

1. 在 stdafx.h 中包含 afxadv.h。这包含类 CRecentFileList

#include <afxadv.h>

2. 将 AddToRecentFileList 添加到 CDocument 派生类中,并在打开和保存文档时使用此函数。

void CCMRUTestDoc::AddToRecentFileList(LPCTSTR lpszPathName)
{
    ((CCMRUTestApp*)AfxGetApp())->AddToRecentFileList(lpszPathName);
}

BOOL CCMRUTestDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
    if (!CDocument::OnOpenDocument(lpszPathName))
        return FALSE;
    
    // Add to MRU file list
    AddToRecentFileList(lpszPathName);
    
    return TRUE;
}

BOOL CCMRUTestDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
    // Add to MRU file list
    AddToRecentFileList(lpszPathName);
    
    return CDocument::OnSaveDocument(lpszPathName);
}

3. 为 CWinApp 派生类添加 AddToRecentFileList

void CCMRUTestApp::AddToRecentFileList(LPCTSTR lpszPathName)
{
    // lpszPathName will be added to the top of the MRU list. 
    // If lpszPathName already exists in the MRU list, it will be moved to the top
    if (m_pRecentFileList != NULL)    {
        m_pRecentFileList->Add(lpszPathName);
    }
}
© . All rights reserved.