深入浅出 mfc Command Routing 命令传递
来源:互联网 发布:淘宝评价管理网址 编辑:程序博客网 时间:2024/06/08 06:03
MFC.cpp
#include "stdafx.h" #include "MY.h"extern CMyWinApp theApp;extern void printlpEntries(AFX_MSGMAP_ENTRY *lpEntry);BOOL CCmdTarget::OnCmdMsg(UINT nID,int nCode){AFX_MSGMAP *pMessageMap;AFX_MSGMAP_ENTRY *lpEntry;for (pMessageMap=GetMessageMap();pMessageMap!=NULL;pMessageMap=pMessageMap->_pBaseMessageMap ){lpEntry=pMessageMap->lpEntries;printlpEntries(lpEntry);}return FALSE;} BOOL CWinThread::InitInstance() { cout<<"CWinThread::InitInstance"<<endl; return TRUE; } int CWinThread::Run() { cout<<"CWinThread::Run"<<endl; return 1; } BOOL CWinApp::InitApplication() { cout<<"CWinApp::InitApplication"<<endl; return TRUE; } BOOL CWinApp::InitInstance() { cout<<"CWinApp::InitInstance"<<endl; return TRUE; } int CWinApp::Run() { cout<<"CWinApp::Run"<<endl; return CWinThread::Run(); } BOOL CWnd::Create() { cout<<"CWnd::Create"<<endl; return TRUE; } BOOL CWnd::CreateEx() { cout<<"CWnd::CreateEx"<<endl; PreCreateWindow(); return TRUE; } BOOL CWnd::PreCreateWindow() { cout<<"CWnd::PreCreateWindow"<<endl; return TRUE; } LRESULT CWnd::WindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam) { AFX_MSGMAP *pMessageMap; AFX_MSGMAP_ENTRY *lpEntry; if (nMsg==WM_COMMAND) { if (OnCommand(wParam,lParam)) return 1; else return (LRESULT)DefWindowProc(nMsg,wParam,lParam); } pMessageMap=GetMessageMap(); for (;pMessageMap!=NULL;pMessageMap=pMessageMap->_pBaseMessageMap) { lpEntry=pMessageMap->lpEntries; printlpEntries(lpEntry); } return 0; } LRESULT CWnd::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) { return TRUE; } BOOL CWnd::OnCommand(WPARAM wParam, LPARAM lParam) { return OnCmdMsg(0,0); } BOOL CFrameWnd::OnCommand(WPARAM wParam,LPARAM lParam) { return CWnd::OnCommand(wParam,lParam); } BOOL CFrameWnd::Create() { cout<<"CFrameWnd::Create"<<endl; CreateEx(); return TRUE; } BOOL CFrameWnd::PreCreateWindow() { cout<<"CFrameWnd::PreCreateWindow"<<endl; return TRUE; } CView* CFrameWnd::GetActiveView() const { return m_pViewActive; } BOOL CFrameWnd::OnCmdMsg(UINT nID,int nCode) { CView *pView=GetActiveView(); if (pView->OnCmdMsg(nID,nCode)) { return TRUE; } if (CWnd::OnCmdMsg(nID,nCode)) { return TRUE; } CWinApp * pApp=AfxGetApp(); if (pApp->OnCmdMsg(nID,nCode)) { return TRUE; } return FALSE; } BOOL CDocument::OnCmdMsg(UINT nID,int nCode) { if (CCmdTarget::OnCmdMsg(nID,nCode)) { return TRUE; } return FALSE; } BOOL CView::OnCmdMsg(UINT nID,int nCode) { if (CWnd::OnCmdMsg(nID,nCode)) { return TRUE; } BOOL bHandled=FALSE; bHandled=m_pDocument->OnCmdMsg(nID,nCode); return bHandled; } AFX_MSGMAP * CCmdTarget::GetMessageMap() const { return &CCmdTarget::messageMap; } AFX_MSGMAP CCmdTarget::messageMap= { NULL, &CCmdTarget::_messageEntries[0] }; AFX_MSGMAP_ENTRY CCmdTarget::_messageEntries[]= { {0,0,CCmdTargetid,0,AfxSig_end,0} }; BEGIN_MESSAGE_MAP(CWnd,CCmdTarget) ON_COMMAND(CWndid,0) END_MESSAGE_MAP() BEGIN_MESSAGE_MAP(CFrameWnd,CWnd) ON_COMMAND(CFrameWndid,0) END_MESSAGE_MAP() BEGIN_MESSAGE_MAP(CDocument,CCmdTarget) ON_COMMAND(CDocumentid,0) END_MESSAGE_MAP() BEGIN_MESSAGE_MAP(CView,CWnd) ON_COMMAND(CViewid,0) END_MESSAGE_MAP() BEGIN_MESSAGE_MAP(CWinApp,CWinThread) ON_COMMAND(CWinAppid,0) END_MESSAGE_MAP() CWinApp *AfxGetApp() { return theApp.m_pCurrentWinApp; } LRESULT AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam,CWnd * pWnd) { return AfxCallWndProc(pWnd,hWnd,nMsg,wParam,lParam); } LRESULT AfxCallWndProc(CWnd * pWnd,HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam) { LRESULT lResult=pWnd->WindowProc(nMsg,wParam,lParam); return lResult; }
MFC.H
#include "stdafx.h"#define TRUE 1#define FALSE 0typedef char* LPSTR;typedef const char* LPCSTR;typedef unsigned long DWORD;typedef int BOOL;typedef unsigned short WORD;typedef int INT;typedef unsigned int UINT;typedef long LONG;typedef UINT WPARAM;typedef LONG LPARAM;typedef LONG LRESULT;typedef int HWND;#define WM_COMMAND 0x0111#define WM_CREATE 0x0001#define WM_PAINT 0x000F#define WM_NOTIFY 0x004E#define CObjectid 0xffff#define CCmdTargetid 1#define CWinThreadid 11#define CWinAppid 111#define CMyWinAppid 1111#define CWndid 12#define CFrameWndid 121#define CMyFrameWndid 1211#define CViewid 122#define CMyViewid 1221#define CDocumentid 13#define CMyDocid 131struct AFX_MSGMAP_ENTRY;struct AFX_MSGMAP{AFX_MSGMAP * _pBaseMessageMap;AFX_MSGMAP_ENTRY * lpEntries;};//#define DECLARE_MESSAGE_MAP()\//static AFX_MSGMAP_ENTRY _messageEntries[];\//static AFX_MSGMAP messageMap;\//virtual AFX_MSGMAP *GetMessageMap() const;//////#define BEGIN_MESSAG_MAP(theClass,baseClass) \//AFX_MSGMAP *theClass::GetMessageMap() const \// {return &theClass::messageMap;} \//AFX_MSGMAP theClass::messageMap={&(baseClass::messageMap),(AFX_MSGMAP_ENTRY *)&(theClass::_messageEntries)};\//AFX_MSGMAP_ENTRY theClass::_messageEntries[]={ #define DECLARE_MESSAGE_MAP()\static AFX_MSGMAP_ENTRY _messageEntries[];\static AFX_MSGMAP messageMap;\virtual AFX_MSGMAP * GetMessageMap() const;#define BEGIN_MESSAGE_MAP(theClass,baseClass1)\AFX_MSGMAP* theClass::GetMessageMap() const\{ return &theClass::messageMap;}\AFX_MSGMAP theClass::messageMap={ &(baseClass1::messageMap),(AFX_MSGMAP_ENTRY*)&(theClass::_messageEntries)};\AFX_MSGMAP_ENTRY theClass::_messageEntries[]={#define END_MESSAGE_MAP()\{ 0,0,0,0,AfxSig_end,(AFX_PMSG)0} }; typedef void (*AFX_PMSG)();struct AFX_MSGMAP_ENTRY{UINT nMessgae;UINT nCode;UINT nID;UINT nLastID;UINT nSig;AFX_PMSG pfn;};#include "afxmsg_.h"class CObject{};class CCmdTarget:public CObject{ public: virtual BOOL OnCmdMsg(UINT nID,int nCode); DECLARE_MESSAGE_MAP()};class CWinThread:public CCmdTarget{ public: virtual BOOL InitInstance(); virtual int Run();};class CWnd;class CWinApp:public CWinThread{public:CWinApp * m_pCurrentWinApp;CWnd * m_pMainWnd;virtual BOOL InitApplication();virtual BOOL InitInstance();virtual int Run();DECLARE_MESSAGE_MAP() };class CDocument:public CCmdTarget{ public: virtual BOOL OnCmdMsg(UINT nID,int nCode); DECLARE_MESSAGE_MAP()};class CWnd:public CCmdTarget{public:virtual BOOL Create(); BOOL CreateEx();virtual BOOL PreCreateWindow();virtual LRESULT WindowProc(UINT nMsg,WPARAM wParam,LPARAM lParam);virtual LRESULT DefWindowProc(UINT message,WPARAM wParam,LPARAM lParam);virtual BOOL OnCommand(WPARAM wParam,LPARAM lParam);DECLARE_MESSAGE_MAP()};class CView;class CFrameWnd:public CWnd{ public: CView *m_pViewActive; BOOL Create(); CView * GetActiveView() const; virtual BOOL PreCreateWindow(); virtual BOOL OnCommand(WPARAM wParam,LPARAM lParam); virtual BOOL OnCmdMsg(UINT nID,int nCode); DECLARE_MESSAGE_MAP() friend CView;};class CView:public CWnd{public:CDocument *m_pDocument;virtual BOOL OnCmdMsg(UINT nID,int nCode);DECLARE_MESSAGE_MAP()friend CFrameWnd;};CWinApp *AfxGetApp();LRESULT AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam,CWnd *pWnd);LRESULT AfxCallWndProc(CWnd *pWnd,HWND hWnd,UINT nMsg,WPARAM wParam,LPARAM lParam);
MY.H
#include "stdafx.h"#include "MFC.h"#include "afxmsg_.h"class CMyWinApp:public CWinApp{ public: CMyWinApp(); virtual BOOL InitInstance(); DECLARE_MESSAGE_MAP()};class CMyFrameWnd:public CFrameWnd{ public:CMyFrameWnd();DECLARE_MESSAGE_MAP()};class CMyDoc:public CDocument{ public:DECLARE_MESSAGE_MAP()};class CMyView:public CView{public:DECLARE_MESSAGE_MAP()};
#include "stdafx.h"#include "MY.h" CMyWinApp theApp;CMyWinApp::CMyWinApp(){m_pCurrentWinApp=this;}BOOL CMyWinApp::InitInstance() { cout<<"CMyWinApp::InitInstance"<<endl; m_pMainWnd=new CMyFrameWnd; return TRUE; } CMyFrameWnd::CMyFrameWnd() { Create(); } BEGIN_MESSAGE_MAP(CMyWinApp,CWinApp) ON_COMMAND(CMyWinAppid,0) END_MESSAGE_MAP() BEGIN_MESSAGE_MAP(CMyFrameWnd,CFrameWnd) ON_COMMAND(CMyFrameWndid,0) END_MESSAGE_MAP() BEGIN_MESSAGE_MAP(CMyDoc,CDocument) ON_COMMAND(CMyDocid,0) END_MESSAGE_MAP() BEGIN_MESSAGE_MAP(CMyView,CView) ON_COMMAND(CMyViewid,0) END_MESSAGE_MAP() void printlpEntries(AFX_MSGMAP_ENTRY *lpEntry) { struct { int classid; char * classname; }classinfo[]= { CCmdTargetid,"CCmdTarget", CWinThreadid,"CWinThread", CWinAppid,"CWinApp", CMyWinAppid,"CMyWinApp", CWndid,"CWnd", CFrameWndid,"CFrameWnd", CMyFrameWndid,"CMyFrameWnd", CViewid,"CView", CMyViewid,"CMyView", CDocumentid,"CDocument", CMyDocid,"CMyDoc", 0," " }; for (int i=0;classinfo[i].classid!=0;i++) { if (classinfo[i].classid==lpEntry->nID) { cout<<lpEntry->nID<" "; cout<<classinfo[i].classname<<endl; break; } } }
afxmsg_.h
#pragma onceenum AfxSig{AfxSig_end=0,AfxSig_vv,};#define ON_COMMAND(id,memberFxn) \{WM_COMMAND,0,(DWORD)id,(DWORD)id,AfxSig_vv,(AFX_PMSG)memberFxn},
Main.cpp
// Frame8.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "MY.h"int _tmain(int argc, _TCHAR* argv[]){CWinApp *pApp=AfxGetApp();pApp->InitApplication();pApp->InitInstance();pApp->Run();CMyDoc *pMyDoc=new CMyDoc;CMyView *pMyView=new CMyView;CFrameWnd *pMyFrame=(CFrameWnd *)pApp->m_pMainWnd;pMyFrame->m_pViewActive=pMyView;pMyView->m_pDocument=pMyDoc;cout<<endl<<" pMyFrame received a WM_CREATE ,routing path: "<<endl;AfxWndProc(0,WM_CREATE,0,0,pMyFrame);cout<<endl<<" pMyView received a WM_PAINT ,routing path: "<<endl;AfxWndProc(0,WM_PAINT,0,0,pMyView);cout<<endl<<" pMyView received a WM_COMMAND ,routing path: "<<endl;AfxWndProc(0,WM_COMMAND,0,0,pMyView);cout<<endl<<" pMyFrame received a WM_COMMAND ,routing path: "<<endl;AfxWndProc(0,WM_COMMAND,0,0,pMyFrame);system("pause");return 0;}
0 0
- 深入浅出 mfc Command Routing 命令传递
- MFC中的命令传递(Command Routing)
- MFC深入浅出--命令传递
- 命令传递(源自深入浅出MFC)
- 深入浅出MFC:MFC的CommandRouting(命令传递)
- 深入浅出MFC学习笔记(第三章:MFC六大关键技术之仿真:命令传递) .
- 深入浅出MFC学习笔记(第三章:MFC六大关键技术之仿真:命令传递) .
- 深入浅出MFC学习笔记(第9章:消息映射与命令传递。)
- 深入浅出MFC学习笔记(第9章:消息映射与命令传递。)
- DRM "ioctl" command routing
- MFC 消息映射 消息传递MESSAGE_MAP 读深入浅出MFC感悟
- 深入浅出设计模式-006:命令模式(Command Pattern)
- 浅析Windows消息在mfc中的传递路线------(Command rounting)
- MFC的消息映射与命令传递
- MFC消息映射与命令传递
- MFC - 进程间传递消息命令
- MFC消息映射与命令传递
- MFC消息映射与命令传递
- 《SQL反模式》之读书计划
- 友元函数与友元类
- 安装JDK
- Nagle算法--TCP缓冲区管理算法
- java多线程之入门(2)
- 深入浅出 mfc Command Routing 命令传递
- linux下tomcat搭建
- 计算机网络
- cocos2d-x scrollView 滚动窗口
- IOS开发之UIButton同时设置图片文字以及出发按钮时文字图片的切换
- 混实验室这一年
- iptables
- php5-dev
- Linux 下Sqlite3 的安装及应用