MFC Framework for Visual Studio 2022 - Application Wizard

I am Liyuan Breeze. As a veteran in the IT industry for 25 years, today we will re-examine the MFC framework knowledge under Visual Studio 2022 development tools.

MFC (Microsoft Foundation Class, Microsoft Foundation Class Library) is a collection of C++ classes developed by Microsoft to simplify the development work of programmers. It is a set of object-oriented function libraries, which are provided to users in the form of classes. Note that Microsoft's purpose is to simplify the development work of programmers, which is the key, it is simplification, not replacement. Using these classes can effectively help programmers complete the development of Windows applications.

In Visual Studio 2022, there is something called the MFC application wizard. The MFC application wizard is a wizard tool that assists us in generating source code. It can help us automatically generate source code based on the MFC framework. In each step of the wizard, programmers can select various features according to their needs to realize custom applications. Below we use the MFC application wizard to create an MFC-based single-document interface (SDI) application.

First, open the main interface of Visual Studio 2022, as shown in the figure below.

Start Visual Studio 2022, click the [File] menu, select [New] → [Project] in the New Project dialog box that appears, and select "MFC Application" in the right window.

 The project name is Mfc, and the solution name is MfcApp.

Click the [Create] button, and the MFC application type option setting dialog box appears, select "Single Document" in the drop-down list box of "Application Type", select "MFCstandard" in the drop-down list box of "Project Style", and other Keep the default selection.

Click the [Next] button, and the "Document Template Properties" setting dialog box will appear, keep the default selection.

 Click the [Next] button, and the "User Interface Function" setting dialog box will appear, keep the default selection.   

Click the [Next] button, and the "Advanced Features" setting dialog box will appear, keep the default selection.

Click the [Next] button, the "generated class" setting dialog box appears, keep the default selection.

Click the [Finish] button, and the MFC application wizard will create a new project for us: Mfc.

Now, press the "Ctrl+F5" key to compile and run the program, and you can see the running results.

In this program, without writing any code ourselves, we generated an application with a title bar, a minimize box, a maximize box, a system menu, and an adjustable border. This program is similar to the one I created in my previous post, but with the addition of a menu bar, toolbar, and status bar. This is all generated through the MFC application wizard.

Program framework based on MFC

The MFC library is a C++ interface for developing Windows applications. MFC provides an object-oriented framework, and program developers can develop Windows applications based on this framework. MFC adopts object-oriented design, and encapsulates most of the Windows API into C++ classes, which are provided to program developers in the form of class member functions. .

Let's take a look at the code that the MFC application wizard helped us generate. Click the [View] menu on the Visual Studio menu bar, click the [Class View] menu item, this will open the class view sub-window, so that we can view the class organization structure in the project. Expand the Mfc root node in the "Class Diagram" to see all five classes in the project. If you want to view the member information of a class, you can directly select a class, and the functions and properties owned by the class will be displayed in the lower pane.

In MFC, class names all start with the letter "C". For a single-document application, that is, the "single document" we selected in the application type when creating the project, there is a CMainFrame class, a class named "C+project name+App", and a class named "C+project name +Doc" and a class named "C+ProjectName+View".

As a beginner, when you first come into contact with MFC programs, you must gradually become familiar with these classes generated by the MFC application wizard, as well as the code in the classes. In this way, when reading the code, you can know which classes and which codes are generated by the wizard, and which classes and which codes are not automatically generated.

Double-click on the class name in the class view window, and the header file defining the class will be opened in the code editor window on the right.

We can find that all five classes have a base class. For example, CMfcView is derived from CView; CMainFrame is derived from CFrameWnd. These base classes are all classes in MFC. You can check the help information of these base classes. If you want to view the help of a certain class or function, you can put the current cursor on the position of the class or function, and then press the F1 key to open the corresponding help in MSDN. There is a "Hierarchy Chart" hyperlink at the bottom of the description page of each class in the MSDN help page. Click this link to see the organizational chart of the entire MFC class.  

The following figure is the MFC class organizational chart.

1. A class that inherits from CObject

2. Inherit CCmdTarget class

3. Inherit the class of CWnd

4. Other classes

5. Hierarchy diagram categories

 

From the figure above, it can be found that CFrameWnd is derived from CWnd. In addition, it can also be found that there is also a CView class derived from CWnd. This means that the CMainFrame class and the CMfcView class in this program have a common base class: the CWnd class. The CWnd class is a very important class in MFC, which encapsulates window-related operations.

About the author: Li Yuan Weifeng, born in 1981, senior engineer, master of engineering from Zhejiang University, software engineering project supervisor, has worked as a programmer, software designer, system architect, early Windows programmer, loyal Visual Studio user, C/C++ user The author is a veteran who has studied, worked hard, and struggled in the computer industry for 25 years. He has experienced the UNIX era, the desktop WIN32 era, the Web application era, the cloud computing era, the mobile phone Android era, the big data era, the ICT era, and AI deep learning Era, the age of intelligent machines, I don't know what era there will be in the future, I just remember that this journey is full of hardships and gains, and I am willing to go on with you, full of hope. 

Guess you like

Origin blog.csdn.net/wang2015cn/article/details/132070117