You can't sleep in the middle of the night. It's up to MFC!

Source: Internet
Author: User

Concepts and functions of MFC
1. What is MFC?
Microsoft Foundation Class Library is short for Microsoft basic class library. It encapsulates some data structures in various windowsapi functions and C ++ syntax.
1) MFC is a class library.
2) MFC is also a program framework.
2. Why is MFC used?
Programming Based on the framework, providing programming speed, reducing the development cycle and
Development costs.
Ii. header file of MFC
Afx-application frameword x

The afx... function is a global function in the MFC library.
The double-colon-modified functions can basically be identified as Windows API functions.

Afx. H-contains various header files of MFC
Afxwin. H-includes afx. h and windows. h
Afxext. H-includes support for extended window classes
For example, toolbar and status bar.
3. Application Types of MFC
1. the console program using the MFC Library has a global variable cwinapp theapp entry function, which is different from the entry function of the common console.
Initialize the global function in the afxwininit-MFC library.
2 Library Program
2.1 use the static library program of MFC
2.2 Use the dynamic library program of MFC
1) Use the dynamic MFC library to create your own rule dynamic library
2) use the static MFC library to create your own rule dynamic library
3) extended MFC Library
The extension of the function of the MFC Library must be called only in the environment that supports the MFC library.
3. MFC window Program
3.1 single document view architecture program (SDI)
Cframewnd-framework window class, responsible for managing the main framework window.
Cwinapp-application class, responsible for the entire program execution process.
Cdocument-document class, responsible for data management.
Cview-View window class, responsible for managing View windows and displaying data in view Windows
3.2 multi-document view framework (MDI)
Cmdichildwnd-Sub-Framework Window, responsible for managing the sub-Framework Window
Cmdiframewnd-main framework window class, responsible for managing the main framework window
Cwinapp-application class, responsible for the entire program execution process.
Cdocument-document class, responsible for data management.
Cview-View window class, responsible for managing View windows and displaying data in view Windows
3.3 dialog box framework
Cwinapp-application class, responsible for the entire program execution process.
Cdialog-dialog box window class, responsible for managing the dialog box window.
Iv. Overview of classes in the MFC class library
1 base classes of most classes in the cobject-MFC library. Encapsulates the most basic mechanism in the MFC Library
Runtime class information mechanism
Dynamic Creation Mechanism
2 cve-target-the most basic class of message ing mechanism
3. cwinapp/cwinthread-application class.
4 cdoctemplate-Document Template Class
Csingledoctemplate/cmultidoctemplate
5. cdocument and its subclass-document class, responsible for data management
6 exceptions-exception class, which encapsulates various exceptions of the MFC Program
7. cfile and its subclass-file operation class encapsulate operations on Hard Disk Files
8. The most basic class of all window classes in the cwnd-MFC Library
9 frame windows-framework window class, manages the Framework Window.
10 Control bars-control bar class
11 property sheets-attribute page class (attribute form class)
12 dialog boxes-various dialog box classes to manage various dialog box windows
13 views-View window class, responsible for managing various view Windows
14. Controls-control class: manages various controls and controls
15. CDC, cgdiobject, and its class-plotting class
16 carrary/cmap/clist and its subclass-data structure for C ++
Encapsulation.
17 non-cobject classes mainly encapsulate various data structures
Example: cpoint/crect .....
5. First MFC Program
1. Set the environment
1.1 Delete the entry function (winmain)
1.2 change the header file to afxwin. h
1.3 project --> Settings to use the MFC library.
2 Program startup
2.1 construct theapp Global Object execution Constructor
1) Save theapp address to the status information of the current program module.
2) Save theapp address to the thread status information of the current program.
Note:
Afxgetthread/afxgetapp-returns the theapp object address.
2.2 Program Execution
1) enter the entry function winmain and call it within the function.
Global function afxwinmain
2) Execution Process of afxwinmain
1. Get theapp object address
2. Call afxwininit to initialize the MFC Library
3. Use theapp object address to call virtual member libraries of the application class
The initapplication function initializes the application.
4. Use theapp object address to call virtual member libraries of the application class
Function initinstance registration/creation/display window
5. Use theapp object address to call the application class member selection
Function run message loop
6. Program exit. Use theapp object address to call the application class.
After-care handling of the member virtual function exitinstance

3 Application class (cwinapp)
3.1 member virtual functions
Initapplication/initinstance/run/exitinstance
3.2 member variables
M_pmainwnd-Save the address of the main framework object of the current program

Code:

# Include "stdafx. H "class cmyframewnd: Public cframewnd {}; Class cmywinapp: Public cwinapp {public: cmywinapp (); Virtual bool initinstance (); Virtual int run () ;}; int cmywinapp :: run () {afxmessagebox ("cmywinapp: Run"); Return 0;} cmywinapp: cmywinapp () {}; cmywinapp theapp; // Global Object bool cmywinapp :: initinstance () {// The internal pointer of this function is & theappcmyframewnd * pframe = new cmyframewnd (); pframe-> Create (null, "mfcbase "); this-> m_pmainwnd = pframe; // Save the frame class object address to pframe-> showwindow (sw_show); pframe-> updatewindow (); return true ;}

The general process of implementing the internal mechanism is as follows:

Afx_module_state AAA; // The status information of the current program module afx_module_state/afx_module_thread_stateafx_module_thread_state BBB; // The State information of the current program thread cmywinapp theapp {afx_module_state * pmodulestate = m_pmodulestate; // obtain the address of the global variable AAA afx_module_thread_state * pthreadstate = pmodulestate-> m_thread; // obtain the address pthreadstate of the global variable BBB-> m_pcurrentwinthread = this; // Save & theapp to a member of the global variable BBB. afxgetthread () {afx_module_thread_state * pstate = afxgetmodulethreadstate (); // obtain the address of the global variable BBB cwinthread * pthread = pstate-> m_pcurrentwinthread; return pthread ;}} * ********************************* winmain () {afxwinmain (...) {// The process is controlled by the application class cwinthread * pthread = afxgetthread (); cwinapp * PAPP = afxetapp (); // obtain the above two sentences & theappafxwininit (...); // initialize the MFC Library PAPP-> initapplication () // virtual function, initialize the application pthread-> initinstance () {// return to your own code} pthread-> Run () {// virtual function, internal this pointer is & theapp cwinthread: Run () {// function internal this pointer is & theapp while (no message) {onidle (...) // virtual functions of the application class. (idle processing)} pumpmessage () {// This pointer inside the function is & theapp if (getmessage caught wm_quit) return false;} If (pumpmessage returned false) exitinstance (); // virtual functions of the application class (aftercare )}}}}

 

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.