MFC › 3. Document/View Architecture
Scroll Views, Form Views and Splitters
CScrollView, CFormView and CSplitterWnd.
Explanation
CView is only the base. MFC supplies ready-made specializations:
| View | Use |
|---|---|
CScrollView |
content larger than the window: scrollbars and coordinate mapping done for you |
CFormView |
a view whose body is a dialog template with controls (and DDX) |
CEditView / CRichEditView |
a text / rich-text editor |
CListView, CTreeView |
wrap CListCtrl / CTreeCtrl as whole views (an Explorer-style app) |
CHtmlView |
embeds a web browser control |
CScrollView: call SetScrollSizes(MM_TEXT, CSize(totalWidth, totalHeight)) in OnInitialUpdate. Before OnDraw the framework adjusts the device context so you draw in logical (document) coordinates. For mouse input convert device → logical with CClientDC dc(this); OnPrepareDC(&dc); dc.DPtoLP(&point);. Use GetDeviceScrollPosition() and ScrollToPosition() to read or set the position.
CSplitterWnd: divides a frame into panes, each its own view. Create it in the frame’s OnCreateClient:
CreateStatic(this, rows, cols): fixed panes with a draggable bar;Create(...): dynamic splitter with a split box the user can drag.
Then CreateView(row, col, RUNTIME_CLASS(...), CSize(initialWidth, initialHeight), pContext) fills each pane. Pane views are ordinary views attached to the same document.
Example
A frame with a tree on the left and a scrollable canvas on the right:
#include <afxwin.h>
#include <afxext.h>
#include <afxcview.h>
class CCanvasView : public CScrollView {
DECLARE_DYNCREATE(CCanvasView)
protected:
void OnInitialUpdate() override {
CScrollView::OnInitialUpdate();
SetScrollSizes(MM_TEXT, CSize(2000, 1500)); // virtual size of the drawing
}
void OnDraw(CDC* pDC) override { // coordinates are LOGICAL here
for (int x = 0; x <= 2000; x += 100) { pDC->MoveTo(x, 0); pDC->LineTo(x, 1500); }
for (int y = 0; y <= 1500; y += 100) { pDC->MoveTo(0, y); pDC->LineTo(2000, y); }
pDC->TextOut(110, 110, _T("(100,100)-(200,200) cell"));
}
afx_msg void OnLButtonDown(UINT nFlags, CPoint point) {
CClientDC dc(this);
OnPrepareDC(&dc); // apply the scroll offset
dc.DPtoLP(&point); // device -> logical (document) coordinates
CString s;
s.Format(_T("Document position: %d, %d"), point.x, point.y);
AfxMessageBox(s);
}
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(CCanvasView, CScrollView)
BEGIN_MESSAGE_MAP(CCanvasView, CScrollView)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
class CTreeNav : public CTreeView {
DECLARE_DYNCREATE(CTreeNav)
protected:
void OnInitialUpdate() override {
CTreeView::OnInitialUpdate();
CTreeCtrl& tree = GetTreeCtrl();
tree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);
HTREEITEM root = tree.InsertItem(_T("Project"));
tree.InsertItem(_T("Sources"), root);
tree.InsertItem(_T("Headers"), root);
tree.Expand(root, TVE_EXPAND);
}
};
IMPLEMENT_DYNCREATE(CTreeNav, CTreeView)
class CSplitFrame : public CFrameWnd {
DECLARE_DYNCREATE(CSplitFrame)
protected:
CSplitterWnd m_splitter;
BOOL OnCreateClient(LPCREATESTRUCT, CCreateContext* pContext) override {
if (!m_splitter.CreateStatic(this, 1, 2)) return FALSE; // 1 row, 2 columns
if (!m_splitter.CreateView(0, 0, RUNTIME_CLASS(CTreeNav), CSize(220, 100), pContext)) return FALSE;
if (!m_splitter.CreateView(0, 1, RUNTIME_CLASS(CCanvasView), CSize(400, 100), pContext)) return FALSE;
SetActiveView(static_cast<CView*>(m_splitter.GetPane(0, 1))); // keyboard focus pane
return TRUE;
}
};
IMPLEMENT_DYNCREATE(CSplitFrame, CFrameWnd)
Register it like any template: new CSingleDocTemplate(IDR_MAINFRAME, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CSplitFrame), RUNTIME_CLASS(CCanvasView)).
Common pitfalls
- Drawing in device coordinates inside a
CScrollViewmakes graphics scroll wrongly. Convert mouse points withDPtoLPafterOnPrepareDC. SetScrollSizesmust be called after the view exists (OnInitialUpdate) and again whenever the document size changes (OnUpdate).CFormViewneeds its dialog template to have style Child, no border, and Visible = false, otherwise it looks wrong.OnCreateClientreturningTRUEwithout creating the splitter’s views leaves a frame without client area (blank window).- Pass the same
pContextto everyCreateViewso the views attach to the document. - A splitter pane that isn’t
SetActiveView’d means commands and the Edit menu act on the wrong view.
Win32 vs MFC
Without CScrollView you handle WM_HSCROLL / WM_VSCROLL, SetScrollInfo, SetViewportOrgEx and the bookkeeping by hand. The splitter is MFC-only: Win32 has no splitter control. See GDI and Device Contexts for coordinate mapping modes.
Exercises
1. A click at device point (50, 40) happens while the view is scrolled 200 pixels to the right. What is the logical x with MM_TEXT?
250 (device x plus the horizontal scroll offset). DPtoLP does this for you.