a MFC Analysis of program execution process
1) We know that in the WIN32API program, the entry of the program is the WinMain function, in which we complete the Registration window class, create the window, enter the message loop, and finally, the operating system according to the message sent to the program window of the program window function. In the MFC program, we are not able to find a program entry like WinMain, instead a series of declarations and definitions of derived classes and a global object of a class derived from the CWinApp class. The CWinApp class is called an Application object, and only one application object is allowed in an MFC program. Because the derived object of CWinApp is global, the constructor of this object is called before all other code is run, and because the CWinApp class contains a handle such as HWND, HINSTANCE, and its constructors perform initialization operations on those member data. The so-called initialization here is simply assigning all the handle objects to null.
2) The Afxwinmain function that is automatically linked to the program by the connector after the call to the CWinApp constructor is called, and this function can be considered an entry function for the MFC program. Calling the global AfxGetApp () function in this function to get the Application object, the Afxinit global function is called, and the function is to initialize the relevant handle data members in the Application object using the parameters passed by the operating system to the Afxwinmain function.
3) The Afxwinmain function then calls the Cwinapp::initapplication member function, which is used to initialize the contents of the document portion of the Application object.
4) Then call the CWinApp::InitInstance member function, in which the new operation is used to declare a frame window object on the heap, which causes the constructor of the frame window object to be called, and the CREATE function is called in the frame window constructor, creating the window. The called create function generally sets the WNDCLASS parameter to NULL, which is called by MFC internally by the PreCreateWindow function, in which MFC registers several default WNDCLASS for the frame window's create. The control of the program is then returned to the CWinApp::InitInstance member function, which calls the CWnd::ShowWindow display window and calls Cwnd::updatewindow to send a WM_PAINT message to the window. When the CWinApp::InitInstance member function is called, the CWinApp::Run member function is called by the Afxwinmain function, which creates and processes the message loop, and handles onidle idle processing when there is no message. This completes the entire program's creation process.
5) in the process of running the program, the operating system sends a stream of messages to the application, and is processed by the message loop in the CWinApp::Run and distributed to the relevant window object's DefWindowProc member function, and this member function queries the window object's message map table, If the corresponding item is found, it is handled by the class member function that is enlisted in the message map, otherwise it is sent in the order of message route like the parent class.
6) At the end of the message run, after the user presses the Close button, the operating system sends a WM_CLOSE message to the program, and by default the program calls Destorywindow and sends a wm_destory message. The default action that the application accepts to this message is to call the PostQuitMessage function, which sends the WM_QUIT message. When the program object receives the WM_QUIT message after the message loop ends, the Afxwinmain function calls the Afxterm function to clean up the resource used by the program and ends the entire program.
Summary: All of the above descriptions cover all the processes from start to finish to the end of a program. I believe we are a bit dizzy, the actual programming is not necessary to understand so much, these are mostly byMFCthe internal automatic help us to complete. ActualMFCin the process of programming, actually understandMFCthe execution flow of each function in the program. Sometimes too much investigationMFCThe details will waste our energy, we should focus on the use ofMFCsolve the actual problem.
two VC6 in SDI process of execution of the program
The following is an example of an SDI project in VC6, which shows the implementation process of the SDI in MFC by setting a breakpoint after each function. Record the following and hope to be helpful to those who have doubts about MFC's execution.
1) Csdiapptheapp; Sdi.cpp
2) Csdiapp::csdiapp ()//sdi.cpp
3) boolcsdiapp::initinstance ()//sdi.cpp
4) Csdidoc::csdidoc ()//sdidoc.cpp
5) Cmainframe::cmainframe ()//mainfrm.cpp
6) Boolcmainframe::P Recreatewindow (createstruct& CS)//mainfrm.cpp
7) int cmainframe::oncreate (lpcreatestructlpcreatestruct)//mainfrm.cpp
8) Csdiview::csdiview ()//sdiview.cpp
9) Boolcsdiview::P Recreatewindow (CREATESTRUCT&CS)//sdiview.cpp
Ten) boolcsdidoc::onnewdocument ()//sdidoc.cpp
one) void Csdiview::ondraw (CDC*PDC)//sdiview.cpp
----------------Close the window-------------------------------------
Csdiview::~csdiview ()
Cmainframe::~cmainframe ()
) Csdidoc::~csdidoc ()
Analysis of MFC program execution process