MFC › 1. Getting Started
MFC Class Hierarchy Overview
CObject, CCmdTarget, CWnd and friends.
Explanation
Most MFC classes descend from CObject. A simplified map of the hierarchy:
CObject
├─ CCmdTarget handles commands/messages (message maps)
│ ├─ CWinThread
│ │ └─ CWinApp the application object
│ ├─ CWnd wraps an HWND
│ │ ├─ CFrameWnd / CMDIFrameWnd / CMDIChildWnd
│ │ ├─ CView (CScrollView, CFormView, CListView, CEditView ...)
│ │ ├─ CDialog (CPropertyPage, CPropertySheet)
│ │ └─ CButton, CEdit, CListBox, CComboBox, CListCtrl, CTreeCtrl ... (controls)
│ ├─ CDocument
│ └─ CDocTemplate
├─ CFile (CStdioFile, CMemFile, CSocketFile)
├─ CGdiObject (CPen, CBrush, CFont, CBitmap, CRgn, CPalette)
├─ CDC (CPaintDC, CClientDC, CWindowDC, CMetaFileDC)
├─ CArchive, CException, CMenu, CImageList ...
└─ collections: CArray, CList, CMap, CStringArray, CObArray ...
Value types that are not CObject-derived (cheap, copyable): CString, CPoint, CSize, CRect, CTime, CTimeSpan.
What CObject provides: run-time type information (IsKindOf), dynamic creation, serialization, and diagnostics (Dump, AssertValid). Classes derived from CObject are not copyable by default (the copy constructor is private).
Naming convention: classes C..., members m_..., Hungarian prefixes (p pointer, n int, sz string, h handle). Global helpers start with Afx (AfxMessageBox, AfxGetApp, AfxGetMainWnd).
Example
The value classes work without any window. An MFC console application needs AfxWinInit first:
#include <afxwin.h>
#include <iostream>
int _tmain() {
if (!AfxWinInit(::GetModuleHandle(nullptr), nullptr, ::GetCommandLine(), 0)) return 1;
CPoint pt(30, 40);
CSize sz(100, 50);
CRect rc(CPoint(10, 10), sz); // left, top, right, bottom = 10,10,110,60
std::wcout << L"width=" << rc.Width() << L" height=" << rc.Height() << L"\n";
std::wcout << L"contains pt: " << (rc.PtInRect(pt) ? L"yes" : L"no") << L"\n";
rc.DeflateRect(5, 5); // shrink by 5 on each side
rc.OffsetRect(100, 0); // move right by 100
std::wcout << L"rect: " << rc.left << L"," << rc.top << L" - " << rc.right << L"," << rc.bottom << L"\n";
CRect other(0, 0, 150, 150), common;
BOOL overlap = common.IntersectRect(rc, other); // rc & other
std::wcout << L"intersect: " << overlap << L" " << common.Width() << L"x" << common.Height() << L"\n";
CString s = _T("MFC");
s += _T(" rocks");
std::wcout << (LPCTSTR)s << L" (" << s.GetLength() << L")\n";
return 0;
}
Common pitfalls
- Copying a
CObject-derived object (a document, a view, a dialog) is a compile error by design; pass pointers or references. - Don’t forget
AfxWinInit(orCWinApp) in console programs that use MFC: some classes crash without the MFC state. CRectis normalized only whenleft < rightandtop < bottom; callNormalizeRect()before comparing or testing points in rectangles you built by dragging.CRect::Width()andHeight()are signed and can be negative for a non-normalized rectangle.CWndobjects and theirHWNDhave separate lifetimes: the C++ object can exist without a window (beforeCreate) and the window can outlive a temporaryCWnd*returned byFromHandle.- Temporary
CWnd*/CDC*objects from functions likeGetDlgItemare valid only until the next idle time; don’t store them.
Legacy vs modern
Classes like CArray, CList and CMap predate the STL; in new code use std::vector, std::map, std::unique_ptr and std::wstring alongside MFC where it helps (Using Modern C++ in MFC Projects). Keep CString, CRect, CPoint and CWnd for the API boundary.
Exercises
1. Which class do you derive from to react to menu commands but have no window?
CCmdTarget: it provides message maps and command routing without an HWND. CDocument and CWinApp are examples.