MFC learning Notes II ---- MFC Framework Program and message ing

Source: Internet
Author: User
Tags comparison table

We already know that in a WindowsProgramIn, the winmain function is used as the entry function of the program. A program's design window (set the corresponding properties for the window) --> Registration window --> creation window --> display and update window --> message loop;

However, when creating an MFC program, we didn't see anything like the winmain function, and the previous program framework was quite different from that of MFC. Now, let's take a look at the program framework and program running mechanism in MFC.

Because MFC encapsulates its SDK, the program running logic should be the same.

In the MFC program, the winmain function is also involved, but the winmain function is linked to the program by the linker during program compilation and connection. After knowing the existence of winmain, how does winmain organize together with other classes?

(1) In cxxxapp, there is a global object of theapp object, which indicates the application itself. For the global object theapp, It is the space allocated to it before the program enters the winmain function, and its constructor will be called when it is defined. In the program, we can see that cxxxapp is a subclass of cwinapp. before calling the constructor of cxxxapp, it will call the constructor of its parent class. Program Initialization is completed in the cwinapp constructor.

(2) afxwinmain function: The winmain function is actually implemented by calling the afxwinmain function. AfxwinmainSource codeIn the MFC SourceCodeIn the winmain. cpp file. Afxwinmain is called firstThe afxgetthread function obtains a cwinthread pointer, and then calls the afxgetapp function to obtain a cwinapp pointer.Because cwinapp inherits from cwinthread, The afxgetthread function actually returns the result of the afxgetapp function.The afxgetthread function obtains a cwinthread pointer, and then calls the afxgetapp function to obtain a cwinapp pointer.The obtained two pointers are actually consistent. All point to theapp global object.

(3) initinstance function: Next, pthread and PAPP call three functions (as shown in winmain. CPP, the file where afxwinmain is located ).PAPP-> initapplication ();

Pthread-> initinstance (); pthread-> Run (); three functions.These three functions complete the steps required by the Program: Design window class, register window class, display window class, message loop, and window process function. (By following up on each function, we can find that the three functions ultimately need to call the function shadow of the Win32 program to complete these functions !)

Design and registration window:MFC has predefined some default standard window classes for us, and the registration window class is completed by the afxenddeferregisterclass function. (The afxenddeferregisterclass function first obtains information about the window class. If the window class has been registered, a true value is returned. Otherwise, the registerclass function is called to register the window class. It is the same as the function used in win32sdk !)

Create window: The Window Creation is implemented by the createex function in the cwnd class. In MFC, the create function of the cframewnd class calls the createex function internally, the former is called by the loadframe function in cramewnd !!! During the implementation of the createex function, precreatewindow is called, while the latter is a virtual function. Therefore, the precreatewindow function of the subclass is called, this gives programmers the opportunity to modify the appearance of a window before it is generated. The parameter type is a createstruct struct.

Display and update window: The cxxxapp class has a member variable m_pmainwnd, which is a cwnd type pointer and saves the pointer of the application window. Call the same showwindow and updatewindow functions in the SDK to complete the task.

Message Loop: As mentioned above, pthread-> Run (); this function implements a message loop. In the For Loop in this function, the translatemessage and dispatchmessage functions we know are executed;

Window Process: In the source code of the afxenddeferregisterclass function, there is a line of well-known code: wndcls. lpfnwndproc = defwindowproc; of course, the MFC program does not send all messages to the defwindowproc window for processing. Message ing is used in MFC!

An MFC message response function contains three pieces of information in the program: The function prototype and function implementation, used to associate the macro of the message and message response functions. (Distributed in header files and source files respectively)

The two afx_msg comment Macros in the header file are the prototype Declaration of the message response function. The source file contains two message ing macros: (1) afx_msg_map, which associates the message with the message response function; (2) the implementation code of the message Response Function in the source file.

Implementation Method of message ing mechanism in MFC: a static comparison table of message and message processing functions is defined in each class that can receive and process messages, that is, the message ing table. In the message ing table, messages are paired with corresponding message processing function pointers. The addresses of all messages that can be processed by a class and their corresponding message processing functions are listed in the static table corresponding to this class. When a message needs to be processed, the program only needs to search for the static table of the message and check whether the message function is used in the table to know that the message can be processed in the table. If yes, find the corresponding message processing function based on the static table. Otherwise, the message is sent to its parent class.

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.