📄 open_most_recent2.shtml
字号:
<HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <!-- add your name in the CONTENT field below --> <META NAME="Author" CONTENT="Izidor Gams"> <TITLE>Doc/View - Open Most Recent File 2</TITLE></HEAD><!-- Set background properties --><body background="/fancyhome/back.gif" bgcolor="#FFFFFF"><!-- A word from our sponsors... --><table WIDTH="100%"><tr WIDTH="100%"><td align=center><td></tr></table><CENTER><H3><FONT COLOR="#AOAO99"><!-- Article Title -->Open Most Recent File 2</FONT></H3></CENTER><HR><!-- Author and contact details -->This article was contributed by <!-- Author Email --><A HREF="mailto:izidor.gams@adria.si"><!-- Author Name -->Izidor Gams</A>.<!-- Sample image - gif or jpg --><!-- Text / source code --><p>The article Open most recent file by Adam Solesby discussed how to open the most recently used file using the MRU list.This article develops the idea to starting a MDI application with many recent files opened.<p>With MDI application it is sometimes useful to start a session with more documents opened or perhaps only documents that were active at the end of our last session.MRU list treats all recent files equally and doesn't check whether they were opened or closed when we left.There is a way around if we manage the MRU list by ourselves. First, we should declare some variables and constants:<!-- start a block of source code --><PRE><TT><FONT COLOR="#990000">int nStartMode; // remember the starting mode int nMaxFiles; // number of recent active files to rememberBOOL bCleanMRU; // shall we manage the MRU list ourselves?CRecentFileList* pMRU; // global pointer to MRU list. #define NO_DOCUMENT 1 // don't open new document at all#define LAST_DOCUMENT 2 // open one or more last documents#define MAX_MRUFILES 10 // Number of most recent documents <!-- end the block of source code --></FONT></TT></PRE>We can allow the user to select various starting modes: to open application with an empty document, with documents that were active when we left our last session, or without any document window. The user could also define maximal number of recent active files to be opened. <!-- start a block of source code --><PRE><TT><FONT COLOR="#990000">nStartMode = LAST_DOCUMENT; nMaxFiles = MAX_MRUFILES; bCleanMRU = TRUE;<!-- end the block of source code --></FONT></TT></PRE>Now add the following code to your application's InitInstance(), after the call to ParseCommandLine():<!-- start a block of source code --><PRE><TT><FONT COLOR="#990000">CMyApp::InitInstance(){ . . . // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); //===START OF MODIFICATION BLOCK========================= if (cmdInfo.m_strFileName.IsEmpty()) { // If a file is not specified on the command line // and we don't want to start with an empty document // we will not use shell command to open a document if(nStartMode==NO_DOCUMENT || nStartMode==LAST_DOCUMENT) cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing; } // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The main window has been initialized, so show and update it. pMainFrame->ShowWindow(m_nCmdShow); pMainFrame->UpdateWindow(); pMRU=m_pRecentFileList; // remember the pointer to MRU. // Open documents with our new function if(cmdInfo.m_strFileName.IsEmpty() && (nStartMode == LAST_DOCUMENT)) OpenRecentFiles(nMaxFiles); //===END OF MODIFICATION BLOCK ========================= return TRUE;}<!-- end the block of source code --></FONT></TT></PRE>And here is the procedure (called from the InitInstance() above) that will open all the documents from the MRU list. Put it into the main application class. With the parameter you can limit the number of documents. <!-- start a block of source code --><PRE><TT><FONT COLOR="#990000">void CMyApp::OpenRecentFiles(int n){ for(int i=0;(i<m_pRecentFileList->GetSize() && i<n);i++) { CString strFileName(m_pRecentFileList->m_arrNames[i]); if(strFileName.IsEmpty()) return; OpenDocumentFile(strFileName); }} <!-- end the block of source code --></FONT></TT></PRE>To close the session leaving only the active documents on the MRU list we need to modify it.Put this code at the end of CMainFrame::OnClose(). <!-- start a block of source code --><PRE><TT><FONT COLOR="#990000">void CMainFrame::OnClose() { . . //===START OF MODIFICATION BLOCK========================= if(bCleanMRU) // option to save only the active files { // clear list for(int i=0;i<pMRU->m_nSize;i++) pMRU->Remove(0); // fill list with active files paths POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition(); if(pos!=NULL) { CDocTemplate* pTempl = AfxGetApp()->GetNextDocTemplate(pos); POSITION pos=pTempl->GetFirstDocPosition(); while(pos!=NULL) { CDocument* pDoc = pTempl->GetNextDoc(pos); if(pDoc) pMRU->Add((LPCTSTR)pDoc->GetPathName()); } } } //===END OF MODIFICATION BLOCK========================= CMDIFrameWnd::OnClose(); }<!-- end the block of source code --></FONT></TT></PRE><!-- create more blocks of source code as needed --><!-- demo project --><!-- Zipped source --><!-- Posted / Update date mm/dd/yy - add to the list --><p>Date Posted: <!-- date here -->5/4/98<br>Posted by: <A HREF="mailto:Azathoth@Cyberdude.com"><!-- Author Name -->Pat Laplante</A>.<P><HR><!-- Codeguru contact details --><TABLE BORDER=0 WIDTH="100%"><TR><TD WIDTH="33%"><FONT SIZE=-1><A HREF="http://www.codeguru.com">Goto HomePage</A></FONT></TD><TD WIDTH="33%"><CENTER><FONT SIZE=-2>© 1998 Zafir Anjum</FONT> </CENTER></TD><TD WIDTH="34%"><DIV ALIGN=right><FONT SIZE=-1>Contact me: <A HREF="mailto:zafir@home.com">zafir@home.com</A> </FONT></DIV></TD></TR></TABLE><!-- Counter --><CENTER><FONT SIZE=-2>46</FONT></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -