Anatomy of WINDOWS-MFC Framework Program

Source: Internet
Author: User

Analysis of MFC Framework Program
1.MFC
Microsoft Foundation Class, the Microsoft Basic Library, is a collection of C + + developed by Microsoft that allows us to use it to write Windows programs.
Here are 4 basic classes, and our Visual C + + programs will basically have these 4 classes

2.MFC AppWizard
It is a tool to assist us in generating source code, it can help us to automatically generate MFC-based source code, so that we can customize our own programs.

the WinMain function in 3.MFC
The WinMain function cannot be found in MFC, because MFC encapsulates the invocation of the WinMain function for us, so we do not see that we need to keep track of the program's running process, so that we can fully understand the process of execution of the program, without error, we do not know what the reason.

4. Analysis of the program framework based on MFC
Classes in MFC start with the C letter, the inherited class structure can be viewed in MSDN, below is a partial inheritance structure of CWnd, this class is very important in MFC, you need to understand.

the WinMain function in 5.MFC
When using the Platform SDK, we directly write the WinMain function to start the program, but in MFC, the framework has already encapsulated the WinMain function, we need to analyze the WinMain function call process, we use the VS2013 environment to analyze.
(1). First make a breakpoint at Afxwinmain, the program will run here, here is the file location
C:\Program Files (x86) \microsoft Visual Studio 12.0\vc\atlmfc\src\mfc\appmodul.cpp

(2). In the global Theapp object object and its constructor at the breakpoint, you will find the program here to run these two breakpoints, and finally run the above Afxwinmain breakpoint, indicating that the operating system program is to construct a global object before calling WinMain function, The WinMain function then continues to complete the initialization of the application.

6.AfxWinMain function
In MFC, AFX represents the application Framework application framework functions, which can also be called global functions, where several important functions are analyzed, the path of the file: C:\Program files (x86) \microsoft Visual Studio 12.0\vc\atlmfc\src\mfc\winmain.cpp

Afxwinmain

7.InitInstance function
This function is a virtual function in the base class and invokes the InitInstance function of our own app class at run time.

8. Message Loops
The run function of the CWinThread class in the Afxwinmain function completes the message loop task.

9. Summarize MFC's running process
(1). Start the application with the global Theapp first, so that the this pointer in the CWinApp points to the object.

(2). Call the constructor of the Theapp global object, and the constructor of the base class is called first to complete some initialization of the application.

(3). Enter the WinMain function, get our own class of app pointer in Afxwinmain, use this pointer to invoke the virtual function of InitInstance, based on the principle of polymorphism, at run time is the InitInstance function that our sub-class implements.

Then do the necessary initialization work, including the window class registration, creation, window display and update, the period will be called multiple times
CreateEx function, because if you create an MDI multi-document program you will create multiple windows, such as frame windows, toolbars, status bars, etc...

(4). Enter the message loop. Although we have set the default window callback function, but MFC uses the message map mechanism to process the various messages, when receives the wm_quit message, exits the message, the loop ends.

10. Structure of the document/view class

MFC provides two classes of cdocment and CView, which make up our common MVC architecture, which is analyzed briefly below.
(1). Mind Mapping

(2). Doc and view links

You can see that the document and view are related by correspondence, and the specific relationship needs to be customized according to the actual project.

(3). Features of the document template

You can see that the main function of a document template is to complete the connection and exchange of data between MVC schemas.

11. Window class, the difference between window class object and window
A lot of people confuse each other when they're developing, and here's a brief description:
The C + + window class object is not the same as the window, and the only relationship between them is a variable of a window handle defined inside the C + + window class object that holds the handle to the window associated with the current window class object. When the window is destroyed, the corresponding C + + window class object is not necessarily destroyed, but this window class object inside the serial handle is not pointed to the previous window, depending on the C + + window class object's own life cycle length, but it should be noted that when the C + + Window object is destroyed, The window associated with it will also be destroyed.

12.Windows program running structure diagram
A picture wins thousands of words

12. Summary
The following is my own summary of the mind map, do not do well, please make a lot of corrections, the complete has not been done well, to share to everyone.


All the relevant information can be downloaded:
https://yunpan.cn/cPVZYbZbi3eW2 Access Password 53e0

Anatomy of WINDOWS-MFC Framework Program

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.