新建一个视图类:CMyView,派生自CView
在对话框类CCreateViewDlg上定义一个视图类指针
CMyView *m_pView; 为了使得视图创建在指定的区域,在对话框上放一个静态文本控件,资源ID为IDC_STATIC_VIEW
// OnInitDialog初始化中添加
UINT TargetCtrID = IDC_STATIC_VIEW; CWnd * pWnd = this->GetDlgItem(TargetCtrID); CRect RectTargetCtrl; pWnd->GetWindowRect(RectTargetCtrl); this->ScreenToClient(RectTargetCtrl); m_pView = (CMyView *)RUNTIME_CLASS(CMyView)->CreateObject(); // if (NULL == m_pView) { return FALSE; } m_pView->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, RectTargetCtrl, this, TargetCtrID);
// 在view类中的OnDraw函数中
void CMyView::OnDraw(CDC* pDC) { CDocument* pDoc = GetDocument(); // TODO: add draw code here CRect rt(0, 50, 200, 200); pDC->DrawText(_T("这是在对话框上创建的视图"), &rt, DT_LEFT); }