The execution sequence of the MFC program.

Source: Internet
Author: User

The execution sequence of the MFC program.

1. Create the Application object theApp

The program first produces one (and only one) Application object theApp, that is, a CWinApp object. Once this global object is generated, it executes its constructor, because CMyWinApp constructor is not defined, the constructor of the CWinApp class is executed. This function is defined in the APPCORE. CPP 75th line. You can search for it by yourself. Therefore, the member variables in CWinApp will get the configuration and initial values because of the birth of theApp global object.

2. WinMain debut

When programming with SDK, the entry point of the program is the WinMain function, and we do not see the WinMain function in the MFC program !~ It turns out that she is hidden in the MFC code. After theApp configuration is complete, WinMain comes on stage, slow! Looking at the program, the Code is not connected to the WinMain function! I don't know about this. MFC is already ready and directly added to the application code by the linker. It turns out that she is in APPMODUL. in CPP, we think that after theApp configuration is complete, the program will be transferred to APPMODUL. CPP is coming. So what is the execution? Let's take a look at the code extracted from APPMODUL. CPP:

Extern "C" int WINAPI

_ TWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {// call shared/exported WinMain return AfxWinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow );}

_ TWinMain function "_ t" is a macro prepared to support Unicode.

_ The return value of the tWinMain function is the return value of the AfxWinMain function. The AfxWinMain function is defined in the WINMAIN. CPP 21st line. After a little sorting, you can see what this "program entry point" mainly does:

Int AFXAPI AfxWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {int nReturnCode =-1; CWinApp * pApp = AfxGetApp ();

AfxWinInit (hInstance, hPrevInstance, lpCmdLine, nCmdShow );

PApp-> InitApplication (); pApp-> InitInstance () nReturnCode = pApp-> Run ();

AfxWinTerm (); return nReturnCode ;}

The AfxGetApp () function is used to obtain the CMyWinApp Object Pointer. Therefore, the above functions are equivalent to the following calls:

CMyWinApp: InitApplication (); CMyWinApp: InitInstance () CMyWinApp: Run ();

Therefore, the following code is called: CWinApp: InitApplication (); // because the CMyWinApp has not rewritten InitApplication CMyWinApp: InitInstance () // because the CMyWinApp has rewritten InitInstance CWinApp: Run (); // because the CMyWinApp has not rewritten Run

A friend who has used the SDK to write a program may now smile.

3. AfxWinInit-AFX internal Initialization

AfxWinInit is the first operation after the CWinApp constructor. It mainly performs AFX internal initialization operations to handle many unpredictable problems. It corresponds to AfxWinTerm. This function is defined in the APPINIT. CPP 24th line. It will not be pulled out here. You can find it yourself!

4. Run CWinApp: InitApplication.

The operation after AfxWinInit is pApp-> InitApplication. We know that pApp points to the CMyWinApp object. When you call:

PApp-> InitApplication ();

Equivalent to calling:

CMyWinApp: InitApplication ();

However, you need to know that CMyWinApp inherits from CWinApp, and InitApplication is a virtual function of CWinApp. We have not rewritten it (in most cases, we do not need to rewrite it), so the above operation is equivalent to calling:

CWinApp: InitApplication ();

This function is defined in APPCORE. CPP row 125th. You can find it by yourself! I won't move it out, and the operations in it are all done by MFC for internal management (in fact, I can't understand it, I know this is the case ).

5. Run CWinApp: InitInstance.

After the InitApplication function, AfxWinMain calls pApp-> InitInstance. When the program calls:

PApp-> InitInstance ();

Equivalent to calling:

CMyWinApp: InitInstance ();

However, you must know that CMyWinApp inherits from CWinApp, and InitInstance is a virtual function of CWinApp. Since we have changed it, the above operation is to call the InitInstance function of our (CMyWinApp.

6. CFrameWnd: Create generates the main window (and registers the window class first)

Now we have come to CWinApp: InitInstance. This function first creates a CMyFrameWnd object to generate the main window. Before creating a CMyFrameWnd pair, you must first execute the constructor CMyFrameWnd: CMyFrameWnd (). This function uses the Create FUNCTION to generate a window:

CMyFrameWnd: CMyFrameWnd () {Create (NULL, "Hello MFC", WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu ");}

Here, Create is a member function of CFrameWnd, which will generate a window. All the friends who have used the SDK programming know that to Create the main window, you must first register a window class and specify window attributes, but which window class is used here? The first parameter of the Create FUNCTION (for details about other parameters, refer to MSDN or in-depth introduction to MFC). What does it mean to set the window class to NULL? It means to generate a standard out-of-box window using the built-in air class of MFC. However, our programs generally do not register any window classes! Oh, before the Create FUNCTION generates a window, it will trigger the registration operation of the window class.

Let's dig out what the Create FUNCTION has done first. The Create function is defined in WINFRM. THE 538th rows of CPP (here I won't Copy the code, you can open it by yourself). The function calls the CreateEx function in line 562, because CreateEx is a member function of CWnd, CFrameWnd is followed by CWnd, so CWnd: CreateEx will be called. This function is defined in WINCORE. CPP row 665th. The following code is used:

BOOL CWnd: CreateEx (DWORD dwExStyle, maid, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU handle, LPVOID lpParam) {// allow modification of several common create parameters CREATESTRUCT cs; cs. dwExStyle = dwExStyle; cs. lpszClass = lpszClassName; cs. lpszName = lpszWindowName; cs. style = dwStyle; cs. x = x; cs. y = y; cs. cx = nWidth; cs. cy = nHeight; cs. hwndParent = hWndParent; cs. hMenu = nIDorHMenu; cs. hInstance = AfxGetInstanceHandle (); cs. lpCreateParams = lpParam;

If (PreCreateWindow (cs) {PostNcDestroy (); return FALSE ;}

AfxHookWindowCreate (this); HWND hWnd =: createmediawex (cs. dwExStyle, cs. lpszClass, cs. lpszName, cs. style, cs. x, cs. y, cs. cx, cs. cy, cs. hwndParent, cs. hMenu, cs. hInstance, cs. lpCreateParams );...}

If you have used SDK programming, you may feel a little bit about the above Code. The PreCreateWindows called in the function is a virtual function, which is defined in CWnd and CFrameWnd. The CFrameWnd: PreCreateWindow should be called here because this pointer refers to the object. This function is defined in WINFRM. CPP line 521st. The following code is used:

BOOL CFrameWnd: PreCreateWindow (CREATESTRUCT & cs) {if (cs. lpszClass = NULL) {VERIFY (AfxDeferRegisterClass (AFX_WNDFRAMEORVIEW_REG); cs. lpszClass = _ afxWndFrameOrView; // COLOR_WINDOW background }...}

AfxDeferRegisterClass is a macro defined in AFXIMPL. H. This macro is shown below:

# Define AfxDeferRegisterClass (fClass) AfxEndDeferRegisterClass (fClass)

Note: The macro here is different from the "let alone MFC". The above code is extracted from Visual C ++ 6.0.

AfxEndDeferRegisterClass is defined in WINCORE. CPP row 3,619th. This function is very complex and mainly involves registering five window classes (Wow! How can I use five window classes? I still don't know). The PreCreateWindow member functions of different classes are called at the moment before the window is generated and ready to register the window class. If the specified window class is NULL, the system default class is used. The PreCreateWindow member functions of CWnd and Its Derived classes show the window classes used by the Framework for different functions.

7. window display and update

After CMyFrameWnd: CMyFrameWnd is completed, the window has been generated; the program flow is returned to CMyWinApp: InitInstance, so the ShowWindow function is called to display the window and the UpdateWindow function is called to send the WM_PAINT message. In the SDK program, messages are processed by window functions. Where are window functions and how are they delivered to window functions? Let's start with CWinApp: Run.

8. Execute CWinApp: Run -- the source of live water of program life

After executing the CMyWinApp: InitInstance function, the program goes to the pApp-> Run of the AfxWinMain function. Now we know that this will execute the CWinApp: Run function, this function is defined in APPCORE. line 2 of CPP. The following is the program code:

Int CWinApp: Run () {if (m_pMainWnd = NULL & AfxOleGetUserCtrl () {// Not launched/Embedding or/Automation, but has no main window! TRACE0 ("Warning: m_pMainWnd is NULL in CWinApp: Run-quitting application./n"); AfxPostQuitMessage (0);} return CWinThread: Run ();}

The function calls the CWinThread: Run function. This function is defined in line 456th of THRDCORE. CPP. I will not Copy it here. The function calls the PumpMessage function in Row 3. This function is defined in Row 3 of THRDCORE. CPP. The Code is as follows:

BOOL CWinThread: PumpMessage () {if (! : GetMessage (& m_msgCur, NULL) {return FALSE ;}

// Process this message if (m_msgCur.message! = WM_KICKIDLE &&! PreTranslateMessage (& m_msgCur) {: TranslateMessage (& m_msgCur);: DispatchMessage (& m_msgCur);} return TRUE ;}

The main operation of this function is to send the message from: DispatchMessage to the window function (CWnd: DefWindowProc), but the program generally does not provide any window function, but in AfxEndDeferRegisterClass, before registering the five window classes, you have specified the window function:

Wndcls. lpfnWndProc = DefWindowProc;

Although the window function is specified as the DefWindowProc member function (CWnd: DefWindowProc), the message is actually not sent to this place, but a global function named AfxWndProc.

9. Connect messages and processing functions-Message Map mechanism

At this point, the main window has been generated, waiting for various messages, and then calling the corresponding processing function. However, how can messages and processing functions be connected together? MFC uses the Message Map mechanism (Message ing mechanism) to provide two sets of macros for the "very convenient interface" used by the application. The principle is not clear yet, it cannot be explained here. The main usage is: first, use DECLARE_MESSAGE_MAP () in the class declaration to provide the processing function, for example:

Class CMyFrameWnd: public CFrameWnd {public: CMyFrameWnd (); afx_msg void OnPaint (); // for WM_PAINT afx_msg void OnAbout (); // for WM_COMMAND (IDM_ABOUT) destroy ()}

Then, use the BEBIN_MESSAGE_MAP () and END_MESSAGE_MAP () Macros in any location of the corresponding. CPP file (of course not in the function) to add the corresponding messages, such:

BEGIN_MESSAGE_MAP (CMyFrameWnd, CFrameWnd) ON_COMMAND (IDM_ABOUT, OnAbout) ON_WM_PAINT () END_MESSAGE_MAP ()

Why does a message automatically flow to the specified function after such a macro? The answer lies in the structure design of the Message Map. Find the Message Map simulation program in Chapter 3rd of "let's get down to MFC!

Related Article

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.