問:我想象DevStudio97那樣將不同文檔類型的MRU子菜單分開,如分成最近的工作區和最近打開的文件.我在CodeGuru上找了找,但沒有什麼收獲.
答:這是可以解決的.我保留了CRecentFileList對象,讓MFC框架去管理默認的文檔類型,然後加入了對第二種文檔類型的處理,我准備一步一步說明這個問題:
使用自定義的菜單ID,如ID_FILE_MRU_MYFILE1,你也應該在字符串表中加入ID_FILE_MRU_MYFILE2, ID_FILE_MRU_MYFILE3, ... ID_FILE_MRU_MYFILE##
m_pRecentListMyFiles =new CRecentFileList( 5, "My Recent File List", "MyFile%d", 5 );
m_pRecentListMyFiles->ReadList();
BOOL CMyApp::OnOpenRecentMyFile(UINT nID)
{
int nIndex = nID - ID_FILE_MRU_MYFILE1;
if (OpenDocumentFile((*m_pRecentListMyFiles)[nIndex]) == NULL)
m_pRecentListMyFiles->Remove(nIndex);
return TRUE;
}
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);
}