MFC 学习笔记( 六 )


相关类:
CView及其子类,父类为CWnd类,封装了关于视图窗口的各种操作,以及和文档类的数据交互 。
使用:

MFC 学习笔记

文章插图
命令消息处理顺序:
视图类 -> 框架类 ->应用程序类
对象关系图:
MFC 学习笔记

文章插图

MFC 学习笔记

文章插图
代码:
#include <afxwin.h>#include "resource.h"class CMyView : public CView{ DECLARE_MESSAGE_MAP( )public: void OnDraw(CDC* pDC); afx_msg void OnPaint( ); afx_msg void OnNew( );};BEGIN_MESSAGE_MAP( CMyView, CView )// ON_COMMAND( ID_NEW, OnNew )// ON_WM_PAINT()END_MESSAGE_MAP()void CMyView::OnNew( ){ AfxMessageBox( "视图类处理了WM_COMMAND消息" );}void CMyView::OnPaint( ){ PAINTSTRUCT ps = { 0 }; HDC hdc = ::BeginPaint( this->m_hWnd, &ps ); ::TextOut( hdc, 200, 200, "CMyView::OnPaint", strlen("CMyView::OnPaint") ); ::EndPaint( this->m_hWnd, &ps );}void CMyView::OnDraw( CDC* pDC ){ pDC->TextOut( 100, 100, "CMyView::OnDraw" );}class CMyFrameWnd : public CFrameWnd{ DECLARE_MESSAGE_MAP()public: afx_msg void OnPaint( ); afx_msg int OnCreate( LPCREATESTRUCT pcs ); afx_msg void OnNew( );};BEGIN_MESSAGE_MAP( CMyFrameWnd, CFrameWnd ) ON_WM_PAINT( )// ON_COMMAND( ID_NEW, OnNew ) ON_WM_CREATE( )END_MESSAGE_MAP( )void CMyFrameWnd::OnNew( ){ AfxMessageBox( "框架类处理了WM_COMMAND消息" );}int CMyFrameWnd::OnCreate( LPCREATESTRUCT pcs ){ CMyView* pView = new CMyView; pView->Create( NULL, "MFCView", WS_CHILD|WS_VISIBLE|WS_BORDER, CRect(0,0,200,200), this,AFX_IDW_PANE_FIRST ); this->m_pViewActive = pView; //视图类对象地址 return CFrameWnd::OnCreate( pcs );}void CMyFrameWnd::OnPaint( ){ PAINTSTRUCT ps = { 0 }; HDC hdc = ::BeginPaint( this->m_hWnd, &ps ); ::TextOut( hdc, 100, 100, "我是框架窗口客户区", strlen("我是框架窗口客户区")); ::EndPaint( this->m_hWnd, &ps );}class CMyWinApp : public CWinApp{ DECLARE_MESSAGE_MAP( )public: virtual BOOL InitInstance( ); afx_msg void OnNew( );};BEGIN_MESSAGE_MAP(CMyWinApp,CWinApp) ON_COMMAND( ID_NEW, OnNew )END_MESSAGE_MAP( )void CMyWinApp::OnNew( ){ AfxMessageBox( "应用程序类处理了WM_COMMAND消息" );}BOOL CMyWinApp::InitInstance( ){ CMyFrameWnd* pFrame = new CMyFrameWnd; pFrame->Create( NULL, "MFCView", WS_OVERLAPPEDWINDOW, CFrameWnd::rectDefault,NULL, (CHAR*)IDR_MENU1); this->m_pMainWnd = pFrame; pFrame->ShowWindow( SW_SHOW ); pFrame->UpdateWindow( ); return TRUE;}CMyWinApp theApp;文档类:
MFC 学习笔记

文章插图
程序的创建过程:
MFC 学习笔记

文章插图

MFC 学习笔记

文章插图
代码:
CMyFrameWnd* pFrame = new CMyFrameWnd;CMyDoc* pDoc = new CMyDoc;CCreateContext cct;cct.m_pCurrentDoc = pDoc;//文档类对象地址cct.m_pNewViewClass = RUNTIME_CLASS(CMyView);//&CMyView::classCMyViewpFrame->LoadFrame(... &cct)//函数内部this为pFrame{Create(...,&cct)//函数内部this为pFrame{CreateEx(...&cct)//函数内部this为pFrame{CREATESTRUCT cs;....cs.lpCreateParams = &cct;::CreateWindowEx(...,&cct);//创建主框架窗口}}}//处理框架窗口的WM_CREATE消息CFrameWnd::OnCreate( pcs )//函数内部this为pFrame,参数可以获取::CreateWindowEx的12个参数{CCreateContext* pContext = (CCreateContext*)pcs->lpCreateParams;//获取&cctOnCreateHelper(pcs, pContext)//函数内部this为pFrame,pContext===&cct{OnCreateClient(pcs, pContext)//函数内部this为pFrame,pContext===&cct{CreateView(pContext..)//函数内部this为pFrame,pContext===&cct{CWnd* pView = pContext->m_pNewViewClass->CreateObject();//动态创建视图类对象,并返回对象地址pView->Create(..,pContext)//函数内部this为pView,pContext===&cct{CreateEx(..,pContext)//函数内部this为pView,pContext===&cct{CREATESTRUCT cs;....cs.lpCreateParams = pContext;//pContext===&cct::CreateWindowEx(...,pContext);//创建视图窗口}}}}}}//处理视图窗口的WM_CREATE消息CView::OnCreate( pcs )//函数内部this为pView,参数可以获取::CreateWindowEx函数的12个参数{CCreateContext* pContext = (CCreateContext*)lpcs->lpCreateParams;//获取&cctpContext->m_pCurrentDoc->AddView(pView)//函数内部this为pDoc,参数为视图类对象地址{m_viewList.AddTail(pView);//文档类对象用一个链表成员变量,保存视图类对象地址pView->m_pDocument = this;//视图类对象用一个普通成员变量,保存文档类对象地址}}

经验总结扩展阅读