Adding Most Recently Used (MRU) Files to an SDI/MDI Application

转自: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> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值