Q: I imagine DevStudio97 to separate the MRU submenus for different document types, such as breaking into the most recent workspace and the most recently opened file. I looked for it on the CodeGuru, but I didn't get anything.
A: This can be solved. I've kept the CRecentFileList object, let the MFC framework manage the default document type, and then added a handle to the second document type, and I'm going to go step-by-step through the problem:
Using custom menu IDs, such as Id_file_mru_myfile1, you should also add Id_file_mru_myfile2, Id_file_mru_myfile3, ... in the string table. id_file_mru_myfile##
- To add a new member to the Application class: Crecentfilelist* m_precentlistmyfiles;
- In the initialization InitInstance () Add the following code:
M_precentlistmyfiles =new crecentfilelist (5, "My recent File List", "myfile%d", 5);
M_precentlistmyfiles->readlist ();
- In ExitInstance () add m_precentlistmyfiles->writelist ();
- Add the UI processing Update_command_ui:m_precentlistmyfiles->updatemenu (pCmdUI) to the Id_file_mru_myfile1;
- Add the following code to the application message map: On_command_ex_range (Id_file_mru_myfile1, Id_file_mru_myfile##,onopenrecentmyfile)
- The following is the handler function for the above message:
BOOL cmyapp::onopenrecentmyfile (UINT NID)
{
int nindex = nid-id_file_mru_myfile1;
if (OpenDocumentFile (*m_precentlistmyfiles) [nindex]) = = NULL)
M_precentlistmyfiles->remove (nindex);
return TRUE;
}
- Overloaded cwinapp::addtorecentfilelist ()
void Ck2App::AddToRecentFileList(LPCTSTR lpszPathName)
{
// Somehow determine doc type, I used file extension
CString strExt;
CString strPathName = lpszPathName;
int nPos = strPathName.ReverseFind( '.' );
if ( nPos != -1 )
strExt = strPathName.Mid( nPos + 1 );
if ( strExt.CompareNoCase( "XXX" ) == 0 )
{
if (m_pRecentListMyFiles!= NULL)
{
// fully qualify the path name
TCHAR szTemp[_MAX_PATH];
AfxFullPath(szTemp, lpszPathName);
// then add to recent file list
m_pRecentListMyFiles->Add(szTemp);
}
}
else // Otherwise, let base class put it on default MRU
CWinApp::AddToRecentFileList(lpszPathName);
}
- And, of course, don't forget to delete M_precentlistmyfiles in the application's destructor.