MFC的视图View

概述

MFC视图(View类)提供一个显示数据的窗口,用于和客户交互,父类是CWnd。

视图的使用:1.定义CView类的子类,实现OnDraw()函数 2.调用Create()函数创建,视图的ID用系统提供的AFX_IDW_PANE_FIRST

视图的销毁:CView::PostNcDestroy()会销毁窗口,delete this;

命令消息的映射:

1.Frame窗口首先收到菜单等命令消息,调用CFrameWnd::OnCmdMsg()函数,在函数中获取Frame窗口的活动视图,调用活动视图的OnCmdMsg()函数。

2.调用Frame的窗口本身的OnCmdMsg()函数,获取当前的App,调用App的OnCmdMsg()函数    View->Frame->App

实例

class CMyView:public CView
{
	virtual void OnDraw(CDC* pDC);
	DECLARE_MESSAGE_MAP()
protected:
	afx_msg void OnPaint(); 
};
BEGIN_MESSAGE_MAP(CMyView,CView)
 ON_WM_PAINT()
END_MESSAGE_MAP()
void CMyView::OnDraw(CDC* pDC) 
{
	pDC->TextOutW(100,100,L"Hello OnDraw");
}
void CMyView::OnPaint()
{
	PAINTSTRUCT ps={0};
	CDC* pDC=BeginPaint(&ps);
	OnDraw(pDC);
	pDC->TextOutW(100,200,L"Hello OnPrint");
	EndPaint(&ps);
} 
class CMainFrame:public CFrameWnd
{
public:
	DECLARE_MESSAGE_MAP()
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
BEGIN_MESSAGE_MAP(CMainFrame,CFrameWnd)
	ON_WM_CREATE()
END_MESSAGE_MAP()
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (-1==CFrameWnd::OnCreate(lpCreateStruct))
	{
		return -1;
	}
	//添加视图
	CMyView* mpWndView=new CMyView;
	mpWndView->Create(NULL,L"MyView",WS_CHILD|WS_VISIBLE|WS_BORDER,	CRect(0,0,100,100),this,AFX_IDW_PANE_FIRST);
	SetActiveView(mpWndView);//设置为活动窗口
	return 0;
}
class MFCViewApp :public CWinApp
 {
 public:
	 virtual BOOL InitInstance(); 
 };
 BOOL MFCViewApp::InitInstance()
 {
	 CMainFrame* pFrame=new CMainFrame;
	 pFrame->Create(NULL,L"MFCView");
	 m_pMainWnd=pFrame;
	 m_pMainWnd->ShowWindow(SW_SHOW);
	 m_pMainWnd->UpdateWindow();
	 return TRUE;
 }
 MFCViewApp theApp;

结果:MFC的视图View_第1张图片

你可能感兴趣的:(MFC的视图View)