菠菜的专栏

行动和思考同样重要

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> 

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭