MFC 学习笔记( 三 )

将这几个宏替换:
#include <afxwin.h>class CMyFrameWnd : public CFrameWnd{// DECLARE_MESSAGE_MAP()protected: static const AFX_MSGMAP* PASCAL GetThisMessageMap(); virtual const AFX_MSGMAP* GetMessageMap() const;public: LRESULT OnCreate( WPARAM wParam, LPARAM lParam );};//BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd)// ON_MESSAGE( WM_CREATE, OnCreate )//END_MESSAGE_MAP()const AFX_MSGMAP* CMyFrameWnd::GetMessageMap() const{ return GetThisMessageMap();}const AFX_MSGMAP* PASCAL CMyFrameWnd::GetThisMessageMap(){static const AFX_MSGMAP_ENTRY _messageEntries[] ={{ WM_CREATE, 0, 0, 0, AfxSig_lwl, (AFX_PMSG)(AFX_PMSGW)(static_cast< LRESULT (AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM) > (&OnCreate)) },{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }};static const AFX_MSGMAP messageMap = { &CFrameWnd::GetThisMessageMap, &_messageEntries[0] };return &messageMap;}LRESULT CMyFrameWnd::OnCreate( WPARAM wParam, LPARAM lParam ){ AfxMessageBox( "WM_CREATE" ); return 0;}class CMyWinApp : public CWinApp{public: virtual BOOL InitInstance( );};CMyWinApp theApp;//爆破点BOOL CMyWinApp::InitInstance( ){CMyFrameWnd* pFrame = new CMyFrameWnd;pFrame->Create(NULL, "MFCCreate");m_pMainWnd = pFrame;pFrame->ShowWindow( SW_SHOW );pFrame->UpdateWindow( );return TRUE;}AFX_MSGMAP_ENTRY结构体的解释:

MFC 学习笔记

文章插图
AFX_MSGMAP结构体的解释:
MFC 学习笔记

文章插图
展开各部分作用介绍:
MFC 学习笔记

文章插图
创建窗口的原理:
MFC 学习笔记

文章插图
代码:
//以WM_CREATE消息为例,捎带想着点WM_PAINT,WM_COMMAND(到了CWnd::OnWndMsg函数路线不一样)AfxWndProc(...){CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);//获取和hWnd绑定在一起的框架类对象地址(pFrame===pWnd)AfxCallWndProc(pWnd...)//参数pWnd===pFrame{pWnd->WindowProc(...)//函数内部this为pFrame===pWnd *************{OnWndMsg(...)//函数内部this为pFrame===pWnd{const AFX_MSGMAP* pMessageMap = GetMessageMap();//获取本类宏站开的静态变量的地址(链表头结点)const AFX_MSGMAP_ENTRY* lpEntry;for (; pMessageMap->pfnGetBaseMap != NULL;//此for就是在遍历链表pMessageMap = (*pMessageMap->pfnGetBaseMap)()){lpEntry = AfxFindMessageEntry(pMessageMap->lpEntries,message, 0, 0));//如果找到返回找到的数组元素的地址,如果没找到返回NULLif(lpEntry != NULL ){goto LDispatch;}}LDispatch:lpEntry->pfn; //CMyFrameWnd::OnCreate调用CMyFrameWnd::OnCreate函数完成消息的处理}}}}三.MFC消息:消息的分类:
MFC 学习笔记

文章插图
示例:
#include <afxwin.h>#define WM_MYMESSAGE WM_USER+1001class CMyFrameWnd : public CFrameWnd{ DECLARE_MESSAGE_MAP()public: int OnCreate( LPCREATESTRUCT pcs); void OnPaint( ); void OnMouseMove( UINT nKey, CPoint pt ); LRESULT OnMyMessage( WPARAM wParam, LPARAM lParam ); int m_x; int m_y;};BEGIN_MESSAGE_MAP(CMyFrameWnd, CFrameWnd) ON_WM_CREATE() ON_WM_PAINT() ON_WM_MOUSEMOVE() ON_MESSAGE( WM_MYMESSAGE,OnMyMessage)END_MESSAGE_MAP()LRESULT CMyFrameWnd::OnMyMessage( WPARAM wParam, LPARAM lParam ){ CString str; str.Format( "wParam=%d,lParam=%d", wParam, lParam ); AfxMessageBox( str ); return 0;}void CMyFrameWnd::OnMouseMove( UINT nKey, CPoint pt){ m_x = pt.x; m_y = pt.y; ::InvalidateRect( this->m_hWnd, NULL, TRUE );}void CMyFrameWnd::OnPaint( ){ PAINTSTRUCT ps = { 0 }; HDC hdc = ::BeginPaint( this->m_hWnd, &ps ); ::TextOut( hdc, m_x, m_y, "hello", 5); ::EndPaint( m_hWnd, &ps );}int CMyFrameWnd::OnCreate( LPCREATESTRUCT pcs ){ AfxMessageBox("WM_CREATE消息被处理"); ::PostMessage( this->m_hWnd, WM_MYMESSAGE, 1, 2 ); return CFrameWnd::OnCreate( pcs );}class CMyWinApp : public CWinApp{public: virtual BOOL InitInstance( );};CMyWinApp theApp;//爆破点BOOL CMyWinApp::InitInstance( ){CMyFrameWnd* pFrame = new CMyFrameWnd;pFrame->Create(NULL, "MFCCreate");m_pMainWnd = pFrame;pFrame->ShowWindow( SW_SHOW );pFrame->UpdateWindow( );return TRUE;}

经验总结扩展阅读