Skip to content

Microsoft Foundation Classes

마이크로소프트 파운데이션 클래스 라이브러리 (Microsoft Foundation Class Library, MFC)는 마이크로소프트에서 만든 윈도 API를 C++로 둘러 싼 라이브러리이다. 클래스들은 윈도의 공용 컨트롤과 스마트 포인터를 사용하는 창 개체이다.

MFC가 처음 도입되었을 때 마이크로소프트는 기본적인 cplusplus 문법에 메시지 처리, 예외 처리, 런타임 처리, 동적 클래스 객체 생성을 위한 매크로를 추가해 C++ 언어를 확장하려 했다. 이런 매크로를 활용한 시스템은 컴파일러의 타입 검사를 무시하므로 버그를 가져올 수 있었다. 32비트 버전의 MFC부터 이것이 개선되었다.

MFC의 가장 큰 장점은 윈도 API를 객체지향적으로 프로그래밍할 수 있다는 것이다. 또한 윈도의 자원과 연관된 형들은 그들을 생성한 객체가 죽게 되면 자동으로 핸들을 해제해 준다는 장점도 있다. 게다가 MFC의 문서/뷰 프레임워크는 데이터 모델과 유저 인터페이스, 제어 로직을 분리한다는 MVC 디자인을 자연스럽게 만들어준다.

Category

Classes

MessageMap

OnDraw vs OnPaint

  1. OnPaint는 화면출력을 위한 함수이고 OnDraw는 화면출력뿐 아니라 프린터 출력에도 관여되는 함수이다.
  2. OnDraw는 CView::OnPaint()에서 호출된다. 따라서 이벤트 코드 생성시 Parent class(CView)의 OnPaint()를 호출하면 안된다. 3

MFC의 App, MainFrame, View, Doc 클래스 참조 방법

//각각 헤더파일 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

도킹(Docking) 가능한 대화상자에 대한 설명

CFrameWnd 의 생명주기

MFC의 Frame window(CFrameWnd)의 생명주기(Life cycle)에 대하여 설명한다.

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

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

See also

Favorite site

References


  1. Dmswl9028-excel-vlookup.pdf 

  2. Mfc_message_map_-_HyacinthWiki.pdf 

  3. Do not call CView::OnPaint() for painting messages라는 주석이 OnPaint() 자동 생성시 추가된다. 

  4. Visual_studio-c_runtime_library_option.zip