深入浅出 mfc Command Routing 命令传递

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 0

typedef 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    131

struct  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()
};


MY.CPP

#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 once

enum 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值