Windows creation and message mechanism in Win32 and introduction to Win32 Encapsulation by MFC

Source: Internet
Author: User

First, let's look at a figure:

ProgramGo to the main function ----> register the window class (registerclass) ---> Create window ----> capture the message and arrange to distribute it to the corresponding window function (getmessage loop) -----> write a window function wndpro () for processing all messages and commands ()

 

The following shows a more detailed graph of each function.

Let's see how these functions are written:

------------------------------------------------------------------------

 

 

The program enters the main function ----> register the window class (registerclass) ---> Create window ----> capture the message and arrange to distribute it to the corresponding Window Function

Number (getmessage loop) -----> write a window function wndpro () to process all messages and commands ()
////
This is almost a complete window message mechanism!
But in MFC, we encapsulate these functions. Note that! It is not a rigid encapsulation of the functional parts of Win32. First, emphasize this point! Getmssage () and translatemessage () are encapsulated in cwinapp. This class is responsible for program initialization and execution, and registerclass is placed in the initapplication function. Put createwindow in initinstance. Both functions are virtual functions. initinstance is an empty virtual function and must be reloaded, therefore, the initinstance function is rewritten in the app file of our own program. Of course, we have mentioned that this function is not a rigid encapsulation. In this function, we do not use createwindow () in Win32 programs () instead of creating the main window, you can directly use the encapsulated class to create the main window of the desired type, such as frequently-used objects that initialize the cframewnd class. Of course, you can also create cdloalg class objects, these windows are derived from the basic class cwnd. When initializing an instance of these classes, they will call their create function, before this function generates a window, it will trigger the registration of the Wind Window Type and its window process, and then create a window of the registered type. I just want to give a rough look at this process so that the window registration and creation process in wind32 corresponds. The actual process in MFC is much more complicated. I am just talking about it, detailed de-reading is a simple introduction.
Initapplication only runs once in each program, and our window class only registers once, so we put it inside
Initinstance: every instance of this class runs once. Therefore, every window needs to be created .....
Note: every program in MFC has an ap-type file cmyapp, which is derived from cwinapp. In this class, it calls the initapplication and initinstance virtual functions. since every window initialization method may be different, we will reload the initinstance function in our app file cmyapp to initialize our own window, so that the base class calls initinstance () it is actually the initialization function that the user loads. So every window is different. This is the diversity of virtual functions. Let's take a look at the six key Technology Simulation chapters of MFC, which are well written!
Note: The above getmssage () and translatemessage () functions capture and dispatch messages are encapsulated into the run function of the cwinapp class.

Note: As the base class of all window classes, the cwnd class encapsulates the function for creating the window (the window class uses the registered WND ), the Window Process functions windowproc and defwindowproc are also encapsulated. When the message is captured and then sent to windowproc (the previous process is omitted. Let's look at the afxwndproc function ), if the messagemap (begin_message_map and end_message_map are defined, the windowproc function will let the onwndmsg () function process and find the corresponding function of the message. If not, use the default defwindowproc. Therefore, if we do not map in the message ing macro and do not use the defwindowproc function, we will overwrite the window process to capture and process the message, it is best to overload the defwindowproc function, otherwise the messagemap will be useless. In this function, you must receive the message and specify it as a processing function. Because the rewritten function does not contain the onwndmsg function.
By the way! When a message is found and sent to the class, it looks for the Window Process of the class. If the class does not exist, it looks for the upper class, one layer, if no rewrite is performed, find the windowproc in cwnd. Virtual functions are useful!

Summary of Win32 Encapsulation by the MFC Program (Registration of window classes, creation of windows, and selection of window procedures)ArticleI will explain how the key functions of the Win32 program are encapsulated in the Framework in MFC.

For more information about the Win32 encapsulation of MFC, refer to the following link:

Http://www.vczx.com/tutorial/mfc/mfc1.php and

Http://www.diybl.com/course/3_program/vc/vc_js/2008520/117199.html

 

 

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.