MFC-exit of Application

Source: Internet
Author: User

  1. ApplicationProgramTo exit

     

    After a Windows application is started, it generally enters a message loop, waiting for or processing user input until the user closes the application window and exits the application.

     

    For example, if you press the close button of the Main Window, select "close" from the Execution System menu, or select "exit" from the "file" menu, the main window will be closed.

    When you select "exit" from the "file" menu, the message id_app_exit is sent. MFC implements the function cwinapp: onappexit () to complete the default processing of the command message.

     

    Void cwinapp: onappexit ()

    {

    // Same as double-clicking on main window close box

    Assert (m_pmainwnd! = NULL );

    M_pmainwnd-> sendmessage (wm_close );

    }

    It can be seen that the implementation is to send the wm_close message to the main window. After the wm_close message is processed in the main window, close the window, send the wm_quit message, exit the message loop (see Figure 5-3), and then exit the entire application.

     

  2. Processing wm_close in the border window

     

MFC provides the function cframewnd: onclose to close all kinds of border windows, including not only the SDI border windows (derived from cframewnd), but also the primary border windows of MDI (derived from cmdiframewnd) or close the document border window (derived from cmdichildwnd.

 

The prototype of this function is as follows:

 

Void cframewnd: onclose ()

As shown in Figure 6-1, it first checks whether the window can be closed (m_lpfncloseproc is a member variable of the function pointer type for printing and previewing), and then processes it according to the actual situation:

 

    • If the main window is closed, close all documents of the program, destroy all windows, and exit the program;

       

    • If the main window is not closed, the document border window is closed, and there are two situations: if the document displayed in the window is only displayed in the window, close the document and document window and destroy the window. If the document displayed in this window is also displayed in other document border windows, close and destroy the document window only.

       

The following are some functions involved in processing the wm_close message.

 

    • Bool cdocument: savemodified ()

       

Default Implementation of the virtual function: first, call ismodifed to determine whether the document has been modified. If no modification is made, the system returns the result. Otherwise, the system continues.

 

When you ask whether the user saves the modified document, if the user represents "cancel", false is returned; if the user represents "no", true is returned; if the user represents "yes ", if the disk fails, false is returned. If the disk is successfully saved, true is returned. To save a disk, you first need to get the name of the saved file, then call the virtual function onsavedocument to complete the saving, and use setmodifidflag (false) to set the document to not modified.

 

    • Bool cdocument: onsavedocument (lpctstr lpszpathname)

       

This is a virtual function used to save files. Its implementation function is opposite to opopendocument, but the processing process is similar and described as follows:

 

Open the file pfile according to lpszpathname;

 

Use pfile to construct a carchive object for Data Writing. This object is used to save data to a file;

 

Set the mouse to the time bottle shape;

Use the serialize function to complete serialized writing;

 

After that, restore the Mouse shape.

    • Cwinapp: saveallmodified ()

       

Cwinapp: closealldocuments (bool bendsession)

Both functions traverse the template Manager List and call the member functions with the same name of cdocmanager one by one for the template manager objects in the list:

 

Cdocmanager: saveallmodified ()

Cdocmanager: closealldocuments (bool bendsession)

Both functions traverse the document template list and call the member functions with the same name of cdoctemplate one by one for the template objects in the list:

 

Cdoctemplate: saveallmodified ()

Cdoctemplate: closealldocuments (bool bendsession)

Both functions traverse the document list and call the cdocuemnt member functions one by one for the document objects in the list:

 

Cdocument: savemodified ()

Cdocument: onclosedocument ()

    • Cdocument: savemodified ()

       

Cdocument: onclosedocument ()

Cdocument: savemodified has been explained earlier. Onclosedocument is a virtual function. Its process is 6-2.

 

Get the pointer of all border windows displaying the document through the view corresponding to the Document Object: When the SDI program closes the window, it obtains the main border window; when the MDI program closes the window, obtain the MDI subwindow.

 

Close and destroy the corresponding border window.

If m_bautodelete is true, the object itself is destroyed.

 

    1. Window destruction process

       

    2. Destroywindow

       

      As shown in Figure 6-1 and figure 6-2, the destroy window is completed by calling destroywindow. Destroywindow is a virtual function of the cwnd class. Cwnd implements this function, and cmdichildwnd overrides this function.

       

      (1) cwnd: destroywindow ()

       

      It is mainly called: destroywindow destroys m_hwnd (which must not be empty), destroys its menu, timer, and other cleanup tasks.

       

      : Destroywindow: deactivates the destroyed window, loses the input focus, and sends wm_destroy and wm_ncdestroy messages to the window and Its subwindows. If the destroyed window is a child window and the wm_noparentnotfiy style is not set, the wm_parentnofity message is sent to the parent window.

       

      (2) cmdichildwnd: destroywindow ()

       

      Because the MDI subwindow cannot be destroyed using: destroywindows, cmdichildwnd overwrites this function. cmdichildwnd mainly calls the member function mdidestroy to send the message wm_mdidestroy to the client window (parent window, let the customer window destroy itself.

       

    3. Process wm_destroy messages

       

The ondestroy message processing function processes wm_destroy messages, cframewnd, cmdichildwnd, cwnd, cview, and its derived classes (such as ceditview) and ccontrolbar. Here, we mainly explain the border, document border, and the ondestroy message processing function of the class.

 

    1. Cwnd: ondestroy ()

       

      Call the default processing function default ().

       

    2. Cframewnd: ondestroy ()

       

      First, destroy the toolbar window. Then, set the menu to the default menu. Then, if you want to destroy the main border window, notify the help program that the application will exit, if no program uses winhelp, disable winhelp and call cwnd: ondestroy.

       

    3. Cmdiframewnd: ondestroy ()

       

      First, adjust the boundary type of the customer window. Then, call the ondestroy of the base class cframewnd. In this case, the toolbar window list of the MDI subwindow is empty, so no toolbar window can be destroyed.

       

    4. Cview: ondestroy ()

       

First, determine whether it is the activity view of the border window. If it is, call setactivateview of the border window to make it inactive. Then, call ondestroy of the base class cwnd.

 

        1. Process wm_ncdestroy messages

           

When the non-customer partition of the window is destroyed, the window receives the wm_ncdestroy message, and onncdestroy processes the wm_ncdestroy message. In MFC, onncdestroy is the last member function called when the Windows window is destroyed.

 

Some Derived classes of cwnd and cview provide functions for processing the message. Here we only discuss the implementation of cwnd.

 

    1. Cwnd: onncdestroy ()

       

      First, determine whether the main window of the current thread is the window. If the module is not a DLL, send the wm_quit message to end the program;

       

      Then, determine whether the current thread's activity window is the window. If so, set the activity window to null;

       

      Then, clear the tooltip window and call default to process the wm_ncdestroy message and unsubclass by Windows default, and detach the window handle from the MFC window object );

       

      Finally, call the virtual function postncdestoy.

       

    2. Postncdestoy

       

Cwnd, cframewnd, cview, and ccontrolbar all cover this function. Both the document border window and the border window use cframewnd: postncdestroy.

    • Cwnd: postncdestroy ()

       

The default Implementation of MFC is null.

    • Void cframewnd: postncdestroy ()

       

Call Delete this to destroy this MFC object.

 

    • Void cview: postncdestroy ()

       

Call Delete this to destroy this MFC object.

 

    1. Destructor

       

Delete this causes the destructor to be called. The destructor of cframewnd and cview must be mentioned.

 

    • Cframewnd ::~ Cframewnd ()

       

When creating a border window, add itself to the module-thread state border window list m_framelist. Now, the window object is removed from the list.

 

If necessary, delete the m_phwnddisable array.

 

    • Cview ::~ Cview ()

       

Add yourself to the View list of document objects during visual creation. The object is now removed from the list.

When the application calls closealldocument to close the document. If the parameter is false, it does not actually clear the view from the list, and the final clearance is completed by the destructor.

 

Now, the process of closing the border window is discussed. The following describes how to process the wm_close message based on the actual situation-the close of the SDI window, the close of the MDI main window, and the close of the MDI subwindow.

 

      1. Close of SDI window, MDI main and Child Window

         

Refer to Figure 6-1 to analyze the closing process of SDI window, MDI main window, and Child Window.

 

    1. Close SDI window

       

      In this case, the main window is closed. First, close the Document Object of the application. When calling the onclosedocument virtual function of the document object, the main window (Windows window and MFC window object) is destroyed, and the view and toolbar window are also destroyed. After the main window is destroyed, the main window object of the application is empty, so the wm_quit message is sent to the end program.

       

    2. Close the MDI Main Window

       

      First, close all document objects of the application. When the onclosedocument function of a Document Object closes a document, the document border window and its window port corresponding to the Document Object are destroyed. In this way, all MDI child windows (including child window views) are destroyed, but the main window of the application is still in progress. Then, call the destroywindow member function to destroy the main window itself. destroywindow finds that the destroyed is the main window of the application, and sends the wm_quit message to terminate the program.

       

    3. Close the MDI subwindow (document border window)

       

In this case, the main window is not closed. Determine whether the document object corresponding to the document border window is used by one or more document border windows. If yes, only the document border window (including its subwindow views) is destroyed ); otherwise, close the document. The onclosedocument of the Document Object will destroy the document border window (including its subwindow views ).

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.