转自:http://www.codeproject.com/docview/most_recent_used.asp
<H2>Introduction</H2>
<P>The Most Recently Used (MRU) files list is standard feature of most Windows
applications. This article describes how to add (MRU) support to Windows
(SDI/MDI) application using the class <CODE>CRecentFileList</CODE>.
<CODE>CRecentFileList </CODE>is a <CODE>CObject </CODE>class that supports
control of the most recently used (MRU) file list. Files can be added to or
deleted from the MRU file list, the file list can be read from or written to the
registry or an .INI file, and the menu displaying the MRU file list can be
updated.
<H2>Using the code</H2>
<P>Adding MRU to MFC SDI or MDI is actually not very difficult. I just add
<CODE>AddToRecentFileList</CODE>(<CODE>LPCTSTR lpszPathName</CODE>) to
<CODE>CDocument </CODE>derived class which calls the add the path name to
<CODE>CWinApp</CODE>'s <CODE>CRecentFileList
</CODE>(m_<CODE>pRecentFileList</CODE>). I use SDI for this demo.
<P>1. Iinclude afxadv.h to stdafx.h. This contains the class
<CODE>CRecentFileList</CODE>.</P><PRE>#include <afxadv.h></PRE>
<P>2. Add <CODE>AddToRecentFileList</CODE> to <CODE>CDocument</CODE> derived
class and use this function during opening and saving the document.</P><PRE>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);
}
</PRE>
<P>3 Add <CODE>AddToRecentFileList</CODE> for <CODE>CWinApp</CODE> derived
class.</P><PRE>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);
}
To enable action on MRU you must add the following code. The ID_FILE_MRU_FILE1..ID_FILE_MRU_FILEx are actually the menu ids of the MRU's:
On MainFrm.cpp
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND_RANGE(ID_FILE_MRU_FILE1, ID_FILE_MRU_FILE4, OnFileMruFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
...
void CMainFrame::OnFileMruFile(UINT nID)
{
int nMRUIndex = -1;
switch (nID) {
case ID_FILE_MRU_FILE1:
nMRUIndex = 0;
break;
case ID_FILE_MRU_FILE2:
nMRUIndex = 1;
break;
case ID_FILE_MRU_FILE3:
nMRUIndex = 2;
break;
case ID_FILE_MRU_FILE4:
nMRUIndex = 3;
break;
}
((CCMRUTestApp*)AfxGetApp())->OpenRecentFileList(nMRUIndex);
}
On CMRUTest
void CCMRUTestApp::OpenRecentFileList(int nIndex)
{
if (m_pRecentFileList != NULL) {
CString strFilename;
if (m_pRecentFileList->GetDisplayName(strFilename,
nIndex,
_T("."),
1))
AfxMessageBox(strFilename);
}
}
You must add code to determine the current directory and replece the _T(".") with its value
}
</PRE>
本文介绍如何使用CRecentFileList类为Windows应用添加最近使用的文件列表(MRU)功能。通过简单几步,包括添加必要的头文件、修改文档类及应用程序类,即可轻松实现MRU列表,并响应列表中的文件打开操作。
4376

被折叠的 条评论
为什么被折叠?



