MFC (Microsoft Foundation Class Library)
Microsoft Foundation Class Library (Microsoft Foundation Classes, MFC) is a class library (class libraries) provided by Microsoft, whichencapsulates the Windows API in the form of C + + classes. and contains an application framework to reduce the workload of application developers. Encapsulates classes that contain a large number of Windows handles encapsulating classes and many of the built-in controls and components of Windows.
VC + + Introduction:
VC + + is an integrated development environment developed by Microsoft, the so-called integrated development environment, that is, it can be edited, compiled, debugged, rather than using a variety of tools to rotate operations, flexibility. VC also refers to its internal compiler, the integrated development environment must have a compiler kernel, such as devc++ one of the compiler kernel is gcc. MFC in addition to a class library, or a framework, in VC + + to create a new MFC project, the development environment will automatically help you generate a lot of files, and it uses MFCxx.DLL. XX is a version, it encapsulates the MFC kernel, so you do not see the original SDK programming in your code in the message loop and so on, because the MFC framework to help you encapsulate, so you can focus on the logic of your program, rather than these every time the programming to repeat things, but because it is a universal framework, Without the best targeting, of course, some flexibility and efficiency are lost. However, MFC's package is very shallow, so the loss of efficiency is small.
The most important encapsulation in MFC is the encapsulation of the Win32 API, and the key to understanding MFC is to understand the relationship between Windows Object and MFC object (an instance of a C + + Class), where Windows Object (Windows object) is a Windows operating system object that is represented by a handle under WIN32, whereas MFC object is a C + + object, an instance of a C + + class that encapsulates the C + + of Windows object (Windows objects) object, which does not refer to any C + + object.
MFC object and Windows object are not the same, but the two are closely related;
The following is an example of C + + window objects and window windows, briefly speaking the links:
An MFC Window object is an instance of a C + + CWnd class (or derived class) that is created directly by the program. In program execution it is generated with the invocation of the window class constructor and disappears as the destructor is called. The Windows window, however, is an instance of an internal data structure of the Windows system, identified by a "window handle" that the Windows system creates and assigns system resources to. The Windows window is created by the Create member function of the CWnd class after the MFC window object is created, and the window handle is saved in the m_hwnd member variable of the Window object. Windows window can be destroyed by a program, can also be destroyed by the user's actions;
Some comparisons are made below:
(1) Different data structure:
MFC object is an instance of a corresponding C + + class that is defined by MFC or a programmer; Windows object is the internal structure of a Windows system, referenced by a handle;
(2) different levels:
MFC object is high-level, Windows object is low, MFC object encapsulates most or all of the features of Windows object, and users of MFC object do not need to directly apply Windows The handle (handle) of object uses the Win32 API instead of a member function that references the corresponding MFC object. (3) Different ways of creation:MFC object is created directly by the program through a constructor, and Windows object is created by the corresponding SDK function. MFC, the use of these MFC object, generally in two steps: First, create an MFC object, or create in the stack, or create in the heap, the MFC object handle instance variable is empty, or is not a valid handle. Then, call the member function of MFC Object to create a valid handle for the corresponding handle variable stored in Windows OBJECT,MFC. (4) different scope of use:MFC object is not visible and unavailable to other processes in the system, and when Windows object is created, its handle is global to the entire Windows system. Some handles can be used by other processes. Typically, a process can get the window handle of another process and send a message to that window. For threads of the same process, you can only use MFC object created by this thread, and you cannot use MFC object for other threads. (5) The difference in destruction:MFC object disappears with the invocation of the destructor, but Windows object must be destroyed by the appropriate Windows system functions. Device Description table The CDC class has different objects, and its corresponding HDC handle object may not be destroyed, but is freed. Of course, you can complete the destruction of Windows object in the destructor of MFC object, the GDI class of MFC object is so implemented, but you should see: The destruction of the two is different. How do I convert between MFC object and Windows objects? a handle to the corresponding Windows object can be obtained from an MFC object, and the corresponding handle is generally obtained using the member function of MFC object Getsafehandle . You can create a corresponding MFC object from an existing Windows object, typically using the member function of MFC object Attach or fromhandle to create a permanent object, The latter may be a temporary object. the corresponding relationship between MFC object and Windows object:
Describe |
Windows handle |
MFC Object |
Window |
HWND |
CWnd and Cwnd-derived classes |
Device context |
HDC |
CDC and Cdc-derived classes |
Menu |
HMENU |
CMenu |
Pen |
Hpen |
CGdiObject class, CPen and Cpen-derived classes |
Brush |
Hbrush |
CGdiObject class, CBrush and Cbrush-derived classes |
Font |
Hfont |
CGdiObject class, CFont and Cfont-derived classes |
Bitmap |
Hbitmap |
CGdiObject class, CBitmap and Cbitmap-derived classes |
Palette |
Hpalette |
CGdiObject class, CPalette and Cpalette-derived classes |
Regional |
HRGN |
CGdiObject class, CRgn and Crgn-derived classes |
Image list |
Himagelist |
CImageList and Cimagelist-derived classes |
Sockets |
SOCKET |
Csocket,casynsocket and its derived classes |
Where: GDI objects include bitmap,brush,font,palette,pen,rgn;
(1) Introduction to MFC and comparison between MFC object and Windows object