MFC program execution process in depth analysis

  • 2020-04-02 02:43:30
  • OfStack

In this paper, a more detailed analysis of VC++ program design MFC program execution process, help to deepen the MFC program operation principle of understanding. Share with you for your reference. Specific analysis is as follows:

An MFC program execution process analysis

1) we know that in the WIN32API program, the entry of the program is the WinMain function, in this function we complete the registration of the window class, create a window, enter the message loop, finally by the operating system according to the message sent to the program window to call the window function of the program. In the MFC program, we do not find a program entry like WinMain, but a series of derived class declarations and definitions and a global object 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 CWinApp derived object is global, so the object constructor will be invoked before all the other code to run, and because the CWinApp class contains the HWND, HINSTANCE handle, such as its constructor is carried out for these members of the initialization data, the so-called initialization here just put all the handle to the object value is NULL.

2) after calling the constructor of CWinApp, the AfxWinMain function automatically linked to the program by the connector will be called, and this function can be regarded as the entry function of MFC program. In this function, the global AfxGetApp() function is called to obtain the application object, and the AfxInit global function is called, which initializes the relevant handle data members in the application object using the parameters passed by the operating system to the AfxWinMain function.

3) then the AfxWinMain function calls the CWinApp::InitApplication member function, which is used to initialize the contents of the document part in the application object.

4) then call CWinApp: : InitInstance member function, in the midst of this member function, the use of new operation window object on the heap declare a framework, leading to the frame window object constructor is called, in the middle of the frame window constructor call the Create function to Create a window, and call the Create function the WNDCLASS parameters are set to NULL, so by the MFC internal call PreCreateWindow function, In this function several default wndclasses are registered by the MFC for use by the Create of the frame window. Program control is then returned to the CWinApp::InitInstance member function, which calls the CWnd::ShowWindow display window and calls the CWnd::UpdateWindow to send the WM_PAINT message to the window. After calling the CWinApp::InitInstance member function, the AfxWinMain function calls the CWinApp::Run member function, which creates and handles the message loop and handles OnIdle processing when there is no message. This completes the creation of the entire program.

5) in the operation process of the program, by the operating system a steady stream of sending a Message to the application, and by the CWinApp: : news circulation processing and distribution of the Run window object to the members of the DefWindowProc function, and by the member function query window object of Message mapping table, if the check to the corresponding item, then by the registration of a class member function in the Message map table processing, otherwise, according to the Message sent order as the father of the Route layer class.

6) after the message runs, the user presses the close button, the operating system sends the WM_CLOSE message to the program. By default, the program calls DestoryWindow and sends the WM_DESTORY message. The default operation after the application receives the message 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, and the AfxWinMain function calls the AfxTerm function to clean up the resources used by the program and end the entire program.

Summary: all of the above descriptions cover the entire process of a program from start to finish. I believe you are a little bit confused, the actual programming is not necessary to understand so much, most of these are done by MFC internal automatic help us. In the actual MFC programming process, in fact, understand the MFC procedures in the execution of each function flow. Sometimes going into the details of MFC is a waste of time, and we should focus on solving practical problems with MFC.

VC6 SDI program execution flow

Take the sdi project in VC6 as an example, you can see the execution flow of MFC's sdi by setting breakpoints before each function and then modal execution. The following is the record, I hope to help those who have doubts about the implementation of MFC.

1) CSdiApp theApp;                   / / sdi. CPP
2) CSdiApp: : CSdiApp ()                         / / sdi. CPP
3) BOOL CSdiApp: : InitInstance ()                 / / sdi. CPP
4) CSdiDoc: : CSdiDoc ()                                     / / sdiDoc. CPP
5) CMainFrame: : CMainFrame ()                   / / MainFrm. CPP
6) BOOL CMainFrame: : PreCreateWindow (CREATESTRUCT & cs)       / / MainFrm. CPP
7) int CMainFrame::OnCreate(LPCREATESTRUCT LPCREATESTRUCT)       / / MainFrm. CPP
8) CSdiView: : CSdiView ()                                         / / sdiView. CPP
9) BOOL CSdiView: : PreCreateWindow (CREATESTRUCT & cs)                 / / sdiView. CPP
10) BOOL CSdiDoc: : OnNewDocument ()                               / / sdiDoc. CPP
11) void CSdiView: : ontouch * pDC (CDC)                               / / sdiView. CPP
/ / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- close the window after -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
12) CSdiView: : ~ CSdiView ()
13) CMainFrame: : ~ CMainFrame ()
14) CSdiDoc: : ~ CSdiDoc ()

Interested friends can use breakpoint debugging to test the SDI program execution flow, to deepen the understanding of MFC operation principle, so as to better grasp the Windows program design.


Related articles: