Adding Most Recently Used (MRU) Files to an SDI/MDI Application
2007-10-09 10:19
295 查看
转自: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>
<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>
相关文章推荐
- Add Most Recently Used Files (MRU) List to Windows Applications(在桌面应用程序中添加最近访问文件列表)
- List Information About the Binary Files Used by an Application
- The certificate used to sign ***has either expired or has been revoked. An updated certificate is required to sign and install the application
- Most Recently Used(mru.dat) SQL SERVER 2005
- List Information About the Binary Files Used by an Application
- Most Recently Used (MRU) menu component
- Adding React to an Existing Application
- Drag and Drop -- Adding Drag and Drop to an SWT Application
- 添加react到已存在的项目中(Adding React to an Existing Application)-2
- Preparing for Application and Service Deployment-Adding an HTTPS Endpoint to a Windows Azure Cloud
- List Information About the Binary Files Used by an Application
- 添加react到已存在的项目中(Adding React to an Existing Application)-1
- List Information About the Binary Files Used by an Application
- The certificate used to sign “AppName” has either expired or has been revoked. An updated certificate is required to sign and install the application解决
- 3 Ways to Kill an Application/Activity/Process in Android
- javassit-a mean to manipulate the java bytecode of an application
- git push error:error: insufficient permission for adding an object to repository data
- Unable to add window -- token null is not for an application
- This application has requested the Runtime to terminate it in an unusual way.
- This application has request the Runtime to terminate it in an unusual way.