Microsoft Foundation Classes
lastmod 2025-02-26created 2014-12-0324 edits
The Microsoft Foundation Class Library (MFC) is a library made by Microsoft that wraps the Windows API in C++. Its classes are window objects that use Windows common controls and smart pointers.
When MFC was first introduced, Microsoft tried to extend the C++ language by adding macros for message handling, exception handling, runtime handling, and dynamic class object creation on top of basic cplusplus syntax. A system built on these macros bypasses the compiler's type checking, so it could introduce bugs. This was improved starting with the 32-bit version of MFC.
The biggest advantage of MFC is that it lets you program the Windows API in an object-oriented way. Another advantage is that types associated with Windows resources automatically release their handles when the object that created them dies. In addition, MFC's document/view framework naturally leads to an MVC design, which separates the data model, the user interface, and the control logic.
Category
Classes
- CDialog
- CEdit - Provides the functionality of the Windows edit control
- CFileDialog - Dialog box for opening and closing files
- CString
- CWndResizer
MessageMap
- MSDN Message Map Macros (MFC)
- About C++/MFC message maps (MessageMap macros broken down) 1
OnDraw vs OnPaint
- OnPaint is a function for screen output, while OnDraw is involved not only in screen output but also in printer output.
- OnDraw is called from CView::OnPaint(). Therefore, when generating event code, you must not call the parent class (CView)'s OnPaint(). 2
How to reference the App, MainFrame, View, and Doc classes in MFC
//각각 헤더파일 include
#include "MainFrm.h" //메인프레임 헤더파일
#include "ClassDoc.h" //Doc클래스 헤더파일
#include "ClassView.h" //View를 include 할때는 반드시 Doc 헤더파일이 위에잇어야한다
#include "Class.h" //APP Class 의 헤더파일
//App 클래스
CMainFrame *pMain=(CMainFrame *)AfxGetMainWnd(); //App -> MainFrm
CClassView *pView=(CClassView *)pMain->GetActiveView(); //App -> MainFrm -> View
CClassDoc *pDoc=(CClassDoc *)pMain->GetActiveDocument(); //App -> MainFrm -> Doc
//뷰클래스
void CClassView::OnMenuView()
CClassApp *pApp=(CClassApp *)AfxGetApp(); //View -> App
CMainFrame *pMain=(CMainFrame *)AfxGetMainWnd(); //View -> MainFrm
CClassDoc *pDoc=(CClassDoc *)pMain->GetActiveDocument();//View -> MainFrm -> Doc
CClassDoc *pDoc=(CClassDoc *)GetDocument(); //View -> Doc
//MainFrame 클래스
CClassView *pView=(CClassView *)GetActiveView(); //MainFrm -> View
CClassDoc *pDoc=(CClassDoc *)GetActiveDocument(); //MainFrm -> Doc
CClassApp *pApp=(CClassApp *)AfxGetApp(); //MainFrm -> App
//Doc 클래스
CClassApp *pApp=(CClassApp *)AfxGetApp(); //Doc -> App
CMainFrame *pMain=(CMainFrame *)AfxGetMainWnd(); //Doc -> MainFrm
CClassView *pView=(CClassView *)pMain->GetActiveView();// Doc -> MainFrm -> View
CClassView *pView=(CClassView *)m_viewList.GetHead(); // Doc -> View
Dialog Docking
An explanation of dockable dialog boxes
Life cycle of CFrameWnd
- MSDN Visual Studio 2012: Creating Document Frame Windows
- MSDN Visual Studio 2012: Destroying Frame Windows
Describes the life cycle of the MFC frame window (CFrameWnd).
Destroying Frame Windows
The MFC framework manages window destruction as well as creation for those windows associated with framework documents and views. If you create additional windows, you are responsible for destroying them.
In the framework, when the user closes the frame window, the window's default OnClose handler calls DestroyWindow. The last member function called when the Windows window is destroyed is OnNcDestroy, which does some cleanup, calls the Default member function to perform Windows cleanup, and lastly calls the virtual member function PostNcDestroy. The CFrameWnd implementation of PostNcDestroy deletes the C++ window object. You should never use the C++ delete operator on a frame window. Use DestroyWindow instead.
When the main window closes, the application closes. If there are modified unsaved documents, the framework displays a message box to ask if the documents should be saved and ensures that the appropriate documents are saved if necessary.
AFX ?
One interesting quirk of MFC is the use of "Afx" as the prefix for many functions, macros and the standard precompiled header name "stdafx.h". During early development what became MFC was called "Application Framework Extensions" and abbreviated "Afx". The name Microsoft Foundation Classes (MFC) was adopted too late in the release cycle to change these references.
Microsoft APIs and frameworks
| Graphics | Desktop Window Manager, Direct2D, Direct3D, D3D (extensions), GDI/GDI+, WPF, Silverlight, WinRT XAML, Windows Color System, Windows Image Acquisition, Windows Imaging Component |
| Audio | DirectMusic, DirectSound, DirectX plugin, XACT, Speech API, XAudio2 |
| Multimedia | DirectX (Media Objects), DirectX (Video Acceleration), DirectInput, DirectShow, Image Mastering API, Managed DirectX, Media Foundation, XNA, Windows Media, Video for Windows |
| Web | MSHTML, RSS Platform, JScript, VBScript, BHO, XDR, SideBar Gadgets |
| Data access | Data Access Components, Extensible Storage Engine, ADO.NET, ADO.NET Entity Framework, Sync Framework, Jet Engine, MSXML, OLE DB, OPC |
| Networking | Winsock (LSP), Winsock Kernel, Filtering Platform, Network Driver Interface Specification, Windows Rally, BITS, P2P API, MSMQ, MS MPI, DirectPlay |
| Communication | Messaging API, Telephony API, WCF |
| Administration and management | Win32 console, Windows Script Host, WMI(extensions), PowerShell, Task Scheduler, Offline Files, Shadow Copy, Windows Installer, Error Reporting, Event Log, Common Log File System |
| Component model | COM, COM+, ActiveX, Distributed Component Object Model, .NET Framework |
| Libraries | Base Class Library (BCL), Microsoft Foundation Classes (MFC), Active Template Library (ATL), Windows Template Library (WTL) |
| Device drivers | Windows Driver Model, Windows Driver Foundation (KMDF), Windows Driver Foundation (UMDF), WDDM, NDIS, UAA, Broadcast Driver Architecture, VxD |
| Security | Crypto API (CAPICOM), Windows CardSpace, Data Protection API, Security Support Provider Interface (SSPI) |
| .NET | ASP.NET, ADO.NET, Base Class Library (BCL), Remoting, Silverlight, TPL, WCF, WCS, WPF, WF |
| Software factories | EFx Factory, Enterprise Library, Composite UI, CCF, CSF |
| IPC | MSRPC, Dynamic Data Exchange (DDE), Remoting, WCF |