Analysis of a simple MFC program based on dialog

Create a new project

Open visual studio, I used 2017 here, the creation of other versions is similar
Insert picture description here

The MFC board is installed by default here, and an MFC application named "test" is
Insert picture description here
Insert picture description here
created. To create a dialog-based MFC program, except for the "Dialog-based" in the second window, the rest of the defaults can go to
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
this dialog-based application. The MFC program of the box is created

The relationship between the object classes in the program

After the program is created, what we really need to care about is actually two files. The others are components provided by vs, namely, test.cpp and testDlg.cpp
Insert picture description here
. The specific functions of each file are explained in detail in the ReadMe.txt file.
Insert picture description here
Insert picture description here
Insert picture description here
As the main file of the program, test.cpp is the program source file.
testDlg.cpp is a window file, that is, a visualized window file.
The mfc program requires at least one application class (CtestApp) and one window frame class (CtestDlg), and the two classes together constitute the simplest MFC program

Program execution flow

Open an MFC project, the program execution process is as follows:

1.CtestApp theApp

That is, the global variable definition before the main function

2. Call the constructor

Insert picture description here

3. Enter Winmain function

This function is automatically provided after the project is created. It is not in the created project file, but hidden in the vs installation directory. I referenced a blog to find it, but I couldn't find it. A reference blog will be attached at the end.

4. Initialization

Will run the InitInstance() function in test.cpp
Insert picture description here

It also includes window class registration, window generation, display and update, and message loop
. The functions of the window are completed by the CtestDlg class, including the operations of the remaining controls and windows in the main window.
Insert picture description here

At this point, after the program has finished running, the following effects will appear
Insert picture description here

to sum up

A and complete MFC program, including at least an application class and a window frame class. Don't go into the use of similar controls in the details, the specific implementation of the function, it is still very simple to want to go up, but the specific implementation code in MFC is very unfamiliar, and even looks very complicated.
And the MFC program project is created. I don’t want the windows API program to see the execution process of the program from the beginning to the end. MFC encapsulates a lot of APIs, maybe the amount of code is reduced, but it also brings a lot of learning to beginners such as me. trouble.
I have referred to many blogs and wrote very detailed, limited to my own knowledge, but I still look at it in the cloud. I hope that the follow-up study can have a deeper understanding.

Reference blog link

https://blog.csdn.net/husongchao/article/details/5063786.

https://blog.csdn.net/zhengjuexi4456/article/details/51841557?utm_medium=distribute.pc_relevant_t0.none-task-blog-searchFromBaidu-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-searchFromBaidu-1.channel_param.

Guess you like

Origin blog.csdn.net/xianyudewo/article/details/109026993