How an application remembers the last active document and automatically reopen it?

Ans: This involves two steps, first is to save the last active document when the frame window is about to close and second is to reopen it the next time application is started. Perform the first step in OnClose( ) handler of main frame class as shown below:

CString docstr = “” ;

CMDIChildWnd *pchild = MDIGetActive( ) ;

if ( pchild )

{

CDocument *pdoc = pchild->GetActiveDocument( ) ;

if ( pdoc )

{

docstr = pdoc -> GetPathName( ) ;

}

}

AfxGetApp( )->WriteProfileString ( “Settings”, “LastDocName”, docstr ) ;

The second step, i.e to open the document when the application is executed next time, write the following code in the InitInstance( ) function.

if ( cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew

|| cmdInfo.m_nShellCommand == CCommandLineInfo::FileNothing )

{

CString docname = GetProfileString ( “Settings”, “LastDocName”, “” ) ;

if ( !docname.IsEmpty( ) )

OpenDocumentFile ( docname ) ;

}

Subscribe / Share

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

Powered by Yahoo! Answers