SDI automatically prompts file storage debugging experiences

Source: Internet
Author: User

Problem:

One MFC-SDIProgramWhen it is disabled, the system automatically prompts "whether to save the changes to XXX", but does not want such a prompt. At the same time, it is found that after opening a document and then opening a document, such a prompt will also appear.

 

Solution Process:

1. Insert a breakpoint in void cmainframe: onclose (). The prompt dialog box is generated by cframewndex: onclose.

2. Track the implementation of the cframewndex: onclose () function and find that the implementation of this function is in the afxframewndex. cpp file. Open the file through vs for tracking and find that the prompt is generated in the cframewnd: onclose () function.

3. Continue tracing and find that it is implemented in winfrm. cpp. This function is:

//////////////////////////////////////// /// // <Br/> // cframewnd closing down </P> <p> void cframewnd:: onclose () <br/>{< br/> If (m_lpfncloseproc! = NULL) <br/>{< br/> // if there is a close proc, then defer to it, and return <br/> // after calling it so the frame itself does not close. <br/> (* m_lpfncloseproc) (this); <br/> return; <br/>}</P> <p> // note: only queries the active document <br/> cdocument * pdocument = getactivedocument (); <br/> If (pdocument! = NULL &&! Pdocument-> cancloseframe (this) <br/>{< br/> // document can't close right now -- don't close it <br/> return; <br/>}< br/> cwinapp * PAPP = afxgetapp (); <br/> If (PAPP! = NULL & PAPP-> m_pmainwnd = This) <br/>{< br/> cdatarecoveryhandler * phandler = PAPP-> getdatarecoveryhandler (); <br/> If (phandler! = NULL) & (phandler-> getshutdownbyrestartmanager () <br/> {<br/> // if the application is being shut down by the restartmanager, DO <br/> // a final autosave. this will mark all the documents as not dirty, <br/> // so the saveallmodified call below won't prompt for save. <br/> phandler-> autosavealldocumentinfo (); <br/> phandler-> saveopendocumentlist (); <br/>}</P> <p> // attempt to save all parameters E NT <br/> If (pdocument = NULL &&! PAPP-> saveallmodified () <br/> return; // don't close it </P> <p> If (phandler! = NULL )&&(! Phandler-> getshutdownbyrestartmanager () <br/> {<br/> // if the application is not being shut down by the restartmanager, <br/> // delete any autosaved documents since everything is now fullysaved. <br/> phandler-> deleteallautosavedfiles (); <br/>}</P> <p> // hide the application's windows before closing all thedocuments <br/> PAPP-> hideapplication (); </P> <p> // close all administrative ents first <br/> Papp -> Closealldocuments (false); </P> <p> // don't exit if there are outstanding Component Objects <br/> If (! Afxolecanexitapp () <br/>{< br/> // take user out of control of the app <br/> afxolesetuserctrl (false ); </P> <p> // don't destroy the main window and close down just yet <br/> // (there areoutstanding component (OLE) Objects) <br/> return; <br/>}</P> <p> // there are cases where destroying the specified ents maydestroy the <br/> // Main Window ofthe application. <br/> If (! Afxcontextisdll & PAPP-> m_pmainwnd = NULL) <br/>{< br/> afxpostquitmessage (0); <br/> return; <br/>}</P> <p> // detect the case that this is the last frame on thedocument and <br/> // shut down onclosedocument instead. <br/> If (pdocument! = NULL & pdocument-> m_bautodelete) <br/>{< br/> boolbotherframe = false; <br/> positionpos = pdocument-> getfirstviewposition (); <br/> while (Pos! = NULL) <br/>{< br/> cview * pview = pdocument-> getnextview (POS); <br/> ensure_valid (pview ); <br/> If (pview-> getparentframe ()! = This) <br/>{< br/> botherframe = true; <br/> break; <br/>}< br/> If (! Botherframe) <br/>{< br/> pdocument-> onclosedocument (); <br/> return; <br/>}</P> <p> // allow the document to cleanup before the window isdestroyed <br/> pdocument-> precloseframe (this ); <br/>}</P> <p> // then destroy the window <br/> destroywindow (); <br/>}

 

It is found that the pdocument-> cancloseframe (this) in this function generates a message about whether to save the file.

 

4. Rewrite the virtual function bool cdoc: cancloseframe (cframewnd * pframe) in the doc class as follows:

Bool cocrcharxdoc: cancloseframe (cframewnd * pframe) <br/>{< br/> return true; <br/> // return cricheditdoc: cancloseframe (pframe ); <br/>}

 

Re-compile and find that there is no saving prompt when the SDI is disabled.

5. The reason for saving the prompt for not opening the document for the first time is that the modification of the file is automatically recorded. You only need to set the Automatically Recorded variable to invalid. You can add the following to the ondraw () function of the cview class:Code:

Cocrcharxdoc * pdoc = getdocument (); <br/> pdoc-> setmodifiedflag (0 );

Now the problem is solved.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.