MFC program entry and execution process

Source: Internet
Author: User

Analysis of the execution process of an MFC Program

1) We know that in the WIN32API program, the program entry is the winmain function. In this function, we register the window class, create a window, and enter the message loop, finally, the operating system calls the window function of the program based on the message sent to the program window. In the MFC program, we cannot find a program entry similar to winmain. Instead, we use the Declaration and definition of a series of derived classes and a Global Object of the class derived from the cwinapp class. The cwinapp class is called an application object. Only one application object is allowed in an MFC program. Because the derived object of cwinapp is global, the constructor of this object will be called before all other code is run, because the cwinapp class contains the existence of hwnd, hinstance, and other handles, its constructor executes the initialization operation on these member data, the so-called initialization here only assigns null values to all handle objects.

2) After the cwinapp constructor is called, The afxwinmain function automatically linked to the program by the connector will be called, and this function can be considered as the entry function of the MFC program. In this function, the global afxgetapp () function is called to obtain the application object. Then, the global function afxinit is called, this function uses the parameters passed to the afxwinmain function by the operating system to initialize the relevant handle data members in the Application object.

3) The afxwinmain function then calls the cwinapp: initapplication member function. This member function is used to initialize the content about the document in the Application object.

4) then call the cwinapp: initinstance member function. In this member function, use the new operation to declare a Framework Window object in the heap. As a result, the constructor of the Framework Window object is called, call the create function in the framework window constructor to create a window. The call to the create function sets the wndclass parameter to null. In this way, the MFC calls the precreatewindow function internally, in this function, MFC registers several default wndclasses for the create function of the framework window. At this time, the control of the program is returned to the cwinapp: initinstance member function. The function calls cwnd: showwindow to display the window and calls cwnd: updatewindow to send a wm_paint message to the window. After the cwinapp: initinstance member function is called, The afxwinmain function calls the cwinapp: Run member function to create and process a message loop, in addition, when there is no message, the onidle is idle. So far, the entire program creation process has been completed.
5) during the running process of the program, the operating system continuously sends messages to the application, and the cwinapp :: the defwindowproc member functions in the run statement are processed cyclically and distributed to the corresponding window objects. The member functions query the message ing table of the window objects. If the corresponding items are found, it is processed by the class member function registered in the message ing table. Otherwise, it is sent in the order of message route as the parent class.
6) after the message operation ends, the user presses the close button and the operating system sends the wm_close message to the program. By default, the program calls destorywindow and sends the wm_destory message, after the application receives the message, the default operation is to call the postquitmessage function, which sends the wm_quit message. When the program object receives the wm_quit message, the message loop ends. The afxwinmain function calls the afxterm function to clear resources used by the program and end the entire program.
Summary: All the above descriptions cover all the processes from the beginning, running to the end of a program.I believe you are a little dizzy. In actual programming, there is no need to deeply understand so much. These tasks are automatically completed by MFC. In the actual MFC programming process, you can actually understand the execution process of each function in the MFC program. Sometimes it is a waste of our energy to investigate the details of MFC. We should focus on using MFC to solve practical problems.

  II. Implementation Process of SDI Program in vc6
The following uses the SDI project in vc6 as an example. By setting breakpoint and post-Debug execution before each function, we can see the implementation process of SDI in MFC. The record is as follows. Anyone who has doubts about the execution of MFC is expected to help. 1) csdiapp theapp; // SDI. cpp2) csdiapp: csdiapp () // SDI. cpp3) bool csdiapp: initinstance () // SDI. cpp4) csdidoc: csdidoc () // sdidoc. cpp5) cmainframe: cmainframe () // mainfrm. cpp6) bool cmainframe: precreatewindow (createstruct & CS) // mainfrm. cpp7) int cmainframe: oncreate (maid) // mainfrm. cpp8) csdiview: csdiview () // sdiview. cpp9) bool csdiview: precreatewindow (createstruct & CS) // sdivi Ew. cpp10) bool csdidoc: onnewdocument () // sdidoc. cpp11) void csdiview: ondraw (CDC * PDC) // sdiview. CPP // ---------------- close the window ----------------------------------------- 12) csdiview ::~ Csdiview () 13) cmainframe ::~ Cmainframe () 14) csdidoc ::~ Csdidoc ()

MFC program entry and execution process

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.