-
June 3rd, 2009, 02:13 AM
#1
Accessing the recent file list
In a MFC application you can add the recent files opened easily to the File menu item list. I have found functions for inserting items in the recent file list but is there a way to obtain the file names (and paths) for the recent file list?
They are stored in the register and you can open a register key to read their values but it seems to me that there should be some easier way to do this.
-
June 3rd, 2009, 02:33 AM
#2
Re: Accessing the recent file list
Victor Nijegorodov
-
June 3rd, 2009, 03:48 AM
#3
Re: Accessing the recent file list
I don't see how that answers the question?
I found that article to when I googled it but after reading it I realized that it was not quite the same as my question.
Last edited by Ergodyne; June 3rd, 2009 at 03:50 AM.
-
June 3rd, 2009, 06:17 AM
#4
Re: Accessing the recent file list
Ok, so I figured it out by my self and will post the answer just for reference.
For reading the last opened file I use the following
Code:
CString file = AfxGetApp()->GetProfileStringA(_T("Recent File List"), _T("File1"), NULL);
_tcscpy(m_szFile1, file.GetString());
where m_szFile1 is a char variable of length _MAX_PATH.
Rewriting recent files to the registry uses
Code:
CMyApp* app = (CMyApp*) AfxGetApp();
app->AddToRecentFileList(m_szFile1);
// This is defined in CMyApp to rewrite the recent files to the registry
app->UpdateRecentFiles();
Where CMyApp is the main App (inherited from CWinApp).
In the CMyApp I have defined the following function
Code:
class CMyApp : public CWinApp
{
public:
CMyApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
public:
void UpdateRecentFiles(){m_pRecentFileList->WriteList();}
};
-
June 3rd, 2009, 06:48 AM
#5
Re: Accessing the recent file list
It's an undocumented solution.
1. Derive a class from CRecentFileList, let's name it CMyRecentFileList.
2. In you ckass derived from CWinApp, write your own implementation of the LoadStdProfileSettings. To do that, just copy the CWinApp::LoadStdProfileSettings from the MFC sources. In that implementation modify the code to create the recent file list object form CMyRecentFileList instead of CRecentFileList.
3. From the inside of that class you'll be able to access the theApp.m_pRecentFileList->m_arrNames[i]. That array holds the list of the recent files. Add a public function to that class to retrieve it.
4. You need to add the following header files: afxadv.h, afximpl.h.
Be aware that this technique makes your code dependent of the version of the Visual Studio you're currently using. Because Microsoft did not document the CRecentFile class and the m_arrNames variable , they're free to rename them or replace them with something different in the future.
That dependence is uncertain but it may happen.
Last edited by srelu; June 3rd, 2009 at 06:58 AM.
-
June 3rd, 2009, 07:17 AM
#6
Re: Accessing the recent file list
Originally Posted by srelu
[SIZE=2]Be aware that this technique makes your code dependent of the version of the Visual Studio you're currently using. Because Microsoft did not document the CRecentFile class and the m_arrNames variable , they're free to rename them or replace them with something different in the future.
That dependence is uncertain but it may happen.
If you meant CRecentFileList clas then it is pretty good documented in MSDN: CRecentFileList Members
And, BTW, what is the reason not to use CRecentFileList class rather than implement your own copy of it?
Victor Nijegorodov
-
June 3rd, 2009, 08:09 AM
#7
Re: Accessing the recent file list
Originally Posted by VictorN
If you meant CRecentFileList clas then it is pretty good documented in MSDN: CRecentFileList Members
And, BTW, what is the reason not to use CRecentFileList class rather than implement your own copy of it?
You're right. The variable is public and it can be accessed directly as theApp.m_pRecentFileList->m_arrNames[i].
I was misguided by the fact that I'm just working with an application where I was forced to write my own implementation of the all function call chain starting from OnOpenDocumentFile including the handling of the recent file list. My document names are folder names and I needed next to the document name additional switches (like IncludeSubfolders).
Not to mention that the document/view architecture was designed to handle files, not folders.
-
June 3rd, 2009, 09:36 AM
#8
Re: Accessing the recent file list
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|