What is the application framework in VC

1. Explanation of related concepts

  • MFC
    The Microsoft Foundation Class is a class library provided by Microsoft Corporation. It encapsulates the win32 API in the form of a C ++ class and contains an application framework. Hence, MFC is actually a mix of libraries and frameworks.
  • The difference between libraries and frameworks
    The library contains classes, objects and methods that have been used directly by programmers, while the framework contains other elements such as: Various libraries, resources, etc. And there is a call difference between the library and the framework that can be seen in the detail ofInversion of Control

2. Create an MFC-based project using Visual Studio 2017

Note The "VC ++ Full Declaration" uses the VC ++ 6.0 version, which is already an older version. The user interface is vastly different from vs2017, but the basic process is still the same.

  • First, you need to verify that the MFC development kit is installed
  • When it is installed, click File-New-Project-Visual C ++ - MFC and confirm to enter the MFC setup wizard
  • In the setup wizard interface, select the interface program for a single document as the application type, and then keep other items as the default. Just click to complete the process.

3. MFC framework analysis

Every Windows desktop program needs to recognize the two parts of windowing and message processing. If you're using a framework to develop a Windows desktop program, you'll need to implement these two parts. The MFC framework provides both of these parts. Allow programmers to focus on higher-level development.
MFC implements various classes to complete the windowing function (MFC specific class organization). However, the basic process is to design the window class, register the window class, create the window. Display and update windows, message capture and message processing, the whole execution flow is as follows:

  • The constructor of the derived class object, since the object is global and is therefore prepended to Function Execution.
  • enter function
  • Function call to achieve the function
  • Function CallDerived Function that the design implements to create window classes, register window classes, create windows, display and update windows.
  • The Run function of the derived class implements the collection and processing of messages. Message processing in MFC differs from the message processing method implemented in Note 1. The message processing in Note 1 is to use a window handler function to process any messages received while MFC uses aMessage cardMechanism for implementing message processing. There is a specific message handling function in the message mapping mechanism for each type of message, and each class maintains a message mapping table that can receive and process messages. When a message is received, it checks the table method to find the appropriate processing function to process the message.

The successfully created MFC project can be executed directly. After running, you will see that the program is not a simple window but consists of the main frame window and the visual window. The main frame window contains a simple menu bar. The MFC framework was implemented here, the development of the MFC form program is intended to further develop these implemented things.

Note
1. "VC ++ Full Discussion" uses VC ++ 6.0, which includes MFC library version MFC 6.0 and MFC version MFC 14.0 included in VS2017
2. The location of the source file mentioned in "Detailed explanation of VC ++" may not be applicable in vs2017. For example, the source file containing the WinMain function should be in vs2017 xx \ Microsoft Visual Studio \ 2017 \ Community \ VC \ Tools \ MSVC \ 14.14.26428 \ atlmfc \ src \ mfc, xx is the installation path of vs2017. Assuming the installation path is C: \ Program Files (x86) \, then the source file path is C: \ Program Files (x86) \ Microsoft Visual Studio \ 2017 \ Community \ VC \ Tools \ MSVC \ 14.14.26428 \ atlmfc \ src \ mfc. However, the method mentioned in this book of using breakpoints and call stacks to determine the source file address is still possible.

4. Add a button to the MFC program

  • Create a new MFC project
  • Click View Class View and find the CMainFrame class in the Class View that appears
  • Right click class-add-add variable, add a type of named Private variable
  • Then you need to add a handler function to the click event, go to ClassWizard, select the message label and check that the class name is show upMessage, edit right click function (this is the processing function that corresponds to the message. If not, you can create one, by clicking Create Processing Function Under Edit Code, Add the following code to the end of the function (before the return statement).
  • Click on Run. You can see that a button appeared in the user interface
  • The button's coordinates are relative to the main frame view, so they are not displayed in the upper right corner of the viewport. You can change the button control's parent window to appear in the viewport. The modification method is to define the button in the view class (the name of the view class refers to the name of the project in the form of "C + project name + view") and not in the main frame class, the definition method and definition in the main frame class Die Method is the same. After a successful change, the result should look like this

5. Problems and Solutions

  • No source file found for WinMain
    inSet a breakpoint, click Run, and then click the call stack when it hits the breakpoint. Below is a double click on the function to open the implementation function.
    or you can find the source file directly via the above file path
  • ClassWizard cannot be found
    VS2017, use the keyboard shortcuts directly, can be transferred
  • After the button has been defined in the view class, follow the method in "VC ++ - Full explanation" p.98 in the buttonChange the parent class in the function and you cannot get the effect defined directly in the CMainFrame class
    The MFC library version in vs2017 is different from the MFC library version in VC ++ 6.0. The inheritance relationship may have changed. The parent class of the view class is not the main frame class, but the parent class of the attempted class The parent class of the parent class is the main frame class, functional parameters should be set as follows to follow the set in the class, the effect is the same.

6. Reference link