命令绕行(侯捷MFC)

命令绕行

思路

应用程序类(含有指向应用程序的指针,指向窗口类的指针);
文档类;
窗口类(含有指向视图类的指针);
视图类(含有指向文档类的指针)。
普通WM_开头的消息有消息映射从子类到父类流串,但是对于命令消息,他的流串不仅仅是子类到父类,还有横向的(这里虚函数和上面类的指针起到了决定性的作用)
这里写图片描述

-视图接收到命令消息的处理:
CView->CCmdTarget然后CDocument ->CCmdTarget
-窗口接收到命令消息的处理:
CFrameWnd然后
CView->CCmdTarget然后CDocument ->CCmdTarget然后到CWnd
但是CWnd没有实现实现虚函数所以直接到CCmdTarget然后到App
但是App没有实现虚函数所以直接到CCmdTarget

afxmsg.h

enum AfxSig
{
        AfxSig_end = 0,     // [marks end of message map]
        AfxSig_vv,
};

#define ON_COMMAND(id, memberFxn) \
   { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)memberFxn },

mfc.h

#define TRUE 1
#define FALSE 0

typedef char* LPSTR;
typedef const char* LPCSTR;

typedef unsigned long  DWORD;
typedef int            BOOL;
typedef unsigned char  BYTE;
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 CN_COMMAND     0x1111
#define WM_COMMAND     0x0111  // following windows.h
#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

#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////////
// Window message map handling

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_MESSAGE_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 END_MESSAGE_MAP() \
        { 0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
        };

// Message map signature values and macros in separate header
#include "afxmsg_.h"

class CObject
{
public:
  CObject::CObject()  {
                      }
  CObject::~CObject() {
                      }
};

class CCmdTarget : public CObject
{
public:
  CCmdTarget::CCmdTarget()  {
                            }
  CCmdTarget::~CCmdTarget() {
                            }

  virtual BOOL OnCmdMsg(UINT nID, int nCode);

  DECLARE_MESSAGE_MAP()       // base class - no {
    
    { }} macros
};

typedef void (CCmdTarget::*AFX_PMSG)(void);

struct AFX_MSGMAP_ENTRY  // MFC 4.0 format
{
    UINT nMessage; // windows message
    UINT nCode;    // control code or WM_NOTIFY code
    UINT nID;      // control ID (or 0 for windows messages)
    UINT nLastID;  // used for entries specifying a range of control id's
    UINT nSig;     // signature type (action) or pointer to message #
    AFX_PMSG pfn;  // routine to call (or special value)
};

class CWinThread : public CCmdTarget
{
public:
  CWinThread::CWinThread()  {
                            }
  CWinThread::~CWinThread() {
                            }

  virtual BOOL InitInstance() {
                                cout << "CWinThread::InitInstance \n";
                                return TRUE;
                              }
  virtual int Run() {
                      cout << "CWinThread::Run \n";
                      // AfxWndProc(...);
                      return 1;
                    }
};

class CWnd;

class CWinApp : public CWinThread
{
public:
  CWinApp* m_pCurrentWinApp;
  CWnd* m_pMainWnd;

public:
  CWinApp::CWinApp()  {
                        m_pCurrentWinApp = this;
                      }
  CWinApp::~CWinApp() {
                      }

  virtual BOOL InitApplication() {
                                   cout << "CWinApp::InitApplication \n";
                                   return TRUE;
                                 }
  virtual BOOL InitInstance()    {
                                   cout << "CWinApp::InitInstance \n";
                                   return TRUE;
                                 }
  virtual int Run() {
                      cout << "CWinApp::Run \n";
                      return CWinThread::Run();
                    }

  DECLARE_MESSAGE_MAP()
};

typedef void (CWnd::*AFX_PMSGW)(void);
                // like 'AFX_PMSG' but for CWnd derived classes only

class CDocument : public CCmdTarget
{
public:
  CDocument::CDocument()   {
                           }
  CDocument::~CDocument()  {
                           }

  virtual BOOL OnCmdMsg(UINT nID, int nCode);

  DECLARE_MESSAGE_MAP()
};

class CWnd : public CCmdTarget
{
public:
  CWnd::CWnd()   {
                 }
  CWnd::~CWnd()  {
                 }

  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;       // current active view

public:
  CFrameWnd::CFrameWnd()   {
                           }
  CFrameWnd::~CFrameWnd()  {
                           }
  BOOL Create();
  virtual BOOL PreCreateWindow();
  CView* GetActiveView() const;
  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;

public:
  CView::CView()   {
                   }
  CView::~CView()  {
                   }

  virtual BOOL OnCmdMsg(UINT nID, int nCode);

  DECLARE_MESSAGE_MAP()

  friend CFrameWnd;
};

// global function
CWinApp* AfxGetApp();
LRESULT AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam,
                   CWnd* pWnd); // last param. pWnd is added by JJHOU.
LRESULT AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg, WPARAM wParam,
                       LPARAM lParam);

mfc.cpp

#include "my.h"  // 原該含入 mfc.h 就好,但為了 extern CMyWinApp 所以...

extern CMyWinApp theApp;
extern void printlpEntries(AFX_MSGMAP_ENTRY* lpEntry);

BOOL CCmdTarget::OnCmdMsg(UINT nID, int nCode)
{
    cout << "CCmdTarget::OnCmdMsg()" << endl;
    // Now look through message map to see if it applies to us
    AFX_MSGMAP* pMessageMap;
    AFX_MSGMAP_ENTRY* lpEntry;
    for (pMessageMap = GetMessageMap(); pMessageMap != NULL;
         pMessageMap = pMessageMap->pBaseMessageMap)
    {
            lpEntry = pMessageMap->lpEntries;

            printlpEntries(lpEntry);
    }

    return FALSE;   // not handled
}

BOOL CWnd::Create()
{
    cout << "CWnd::Create \n";
    return TRUE;
}

BOOL CWnd::CreateEx()
{
    cout << "CWnd::CreateEx \n";
    PreCreateWindow();
    return TRUE;
}

BOOL CWnd::PreCreateWindow()
{
  cout << "CWnd::PreCreateWindow \n";
  return TRUE;
}

LRESULT CWnd::WindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
    AFX_MSGMAP* pMessageMap;
    AFX_MSGMAP_ENTRY* lpEntry;

    cout << "CWnd::WindowProc()" << endl;
    if (nMsg == WM_COMMAND) // special case for commands
    {
        if (OnCommand(wParam, lParam))
            return 1L; // command handled
        else
            return (LRESULT)DefWindowProc(nMsg, wParam, lParam);
    }

    pMessageMap = GetMessageMap();
    for (; pMessageMap != NULL;
         pMessageMap = pMessageMap->pBaseMessageMap)
    {
            lpEntry = pMessageMap->lpEntries;
            printlpEntries(lpEntry);
    }
    return 0;  // J.J.Hou: if find, should call lpEntry->pfn,
               // otherwise should call DefWindowProc.
               // for simplization, we just return 0.
}

LRESULT CWnd::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    cout << "CWnd::DefWindowProc()" << endl;
    return TRUE;
}

BOOL CWnd::OnCommand(WPARAM wParam, LPARAM lParam)
{
    cout << "CWnd::OnCommand()" << endl;
    // ...
    return OnCmdMsg(0, 0);
}

BOOL CFrameWnd::OnCommand(WPARAM wParam, LPARAM lParam)
{
    cout << "CFrameWnd::OnCommand()" << endl;
    // ...
    // route as normal command
    return CWnd::OnCommand(wParam, lParam);
}

BOOL CFrameWnd::Create()
{
    cout << "CFrameWnd::Create \n";
    CreateEx();
    return TRUE;
}

BOOL CFrameWnd::PreCreateWindow()
{
    cout << "CFrameWnd::PreCreateWindow \n";
    return TRUE;
}

CView* CFrameWnd::GetActiveView() const
{
    cout << "CFrameWnd::GetActiveView()" << endl;
    return m_pViewActive;
}

BOOL CFrameWnd::OnCmdMsg(UINT nID, int nCode)
{
    cout << "CFrameWnd::OnCmdMsg() called ,search this's MESSAGE_MAP  find message handle "
        "if not finded goto Base Class" << endl;
    // pump through current view FIRST
    CView* pView = GetActiveView();
    if (pView->OnCmdMsg(nID, nCode))
            return TRUE;

    // then pump through frame
    if (CWnd::OnCmdMsg(nID, nCode))
            return TRUE;

    // last but not least, pump through app
    CWinApp* pApp = AfxGetApp();
    if (pApp->OnCmdMsg(nID, nCode))
            return TRUE;

    return FALSE;
}

BOOL CDocument::OnCmdMsg(UINT nID, int nCode)
{
    cout << "CDocument::OnCmdMsg() called ,search this's MESSAGE_MAP  find message handle "
        "if not finded goto Base Class" << endl;
    if (CCmdTarget::OnCmdMsg(nID, nCode))
        return TRUE;

    return FALSE;
}

BOOL CView::OnCmdMsg(UINT nID, int nCode)
{
    cout << "CView::OnCmdMsg() called ,search this's MESSAGE_MAP  find message handle "
        "if not finded goto Base Class" << endl;
    if (CWnd::OnCmdMsg(nID, nCode))
        return TRUE;

    BOOL bHandled = FALSE;
    bHandled = m_pDocument->OnCmdMsg(nID, nCode);
    return bHandled;
}

AFX_MSGMAP* CCmdTarget::GetMessageMap() const  // JJHou: in MFC 40 cmdtarg.cpp
{
    return &CCmdTarget::messageMap;
}

AFX_MSGMAP CCmdTarget::messageMap =   // JJHou: in in MFC 40 cmdtarg.cpp
{
    NULL,
    &CCmdTarget::_messageEntries[0]
};

AFX_MSGMAP_ENTRY CCmdTarget::_messageEntries[] = // JJHou: in in MFC 40 cmdtarg.cpp
{
    { 0, 0, CCmdTargetid, 0, AfxSig_end, 0 }  // nothing here
};

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, CCmdTarget)
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)  // last param. pWnd is added by JJHou.
{
  cout << "AfxWndProc()" << endl;
  return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
}

LRESULT AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
                       WPARAM wParam, LPARAM lParam)
{
  cout << "AfxCallWndProc()" << endl;
  LRESULT lResult = pWnd->WindowProc(nMsg, wParam, lParam);
  return lResult;
}

my.h

#include <iostream>
#include "mfc.h"

class CMyWinApp : public CWinApp
{
public:
  CMyWinApp::CMyWinApp()   {
                           }
  CMyWinApp::~CMyWinApp()  {
                           }
  virtual BOOL InitInstance();
  DECLARE_MESSAGE_MAP()
};

class CMyFrameWnd : public CFrameWnd
{
public:
  CMyFrameWnd();
  ~CMyFrameWnd()  {
                  }
  DECLARE_MESSAGE_MAP()
};

class CMyDoc : public CDocument
{
public:
  CMyDoc::CMyDoc()  {
                    }
  CMyDoc::~CMyDoc() {
                    }
  DECLARE_MESSAGE_MAP()
};

class CMyView : public CView
{
public:
  CMyView::CMyView()   {
                       }
  CMyView::~CMyView()  {
                       }
  DECLARE_MESSAGE_MAP()
};

my.cpp

#include "my.h"

CMyWinApp theApp;  // global object

BOOL CMyWinApp::InitInstance()
{
    cout << "CMyWinApp::InitInstance \n";
    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;
        }
    }
    /*cout << "print message map : \n";
    cout << "nMessage\t nCode\t nID\t nLastID\t nSig\t pfn\n";
    while (lpEntry->nSig != AfxSig_end){
        cout << lpEntry->nMessage << "    ";
        cout << lpEntry->nCode << "    ";
        cout << lpEntry->nID << "    ";
        cout << lpEntry->nLastID << "    ";
        cout << lpEntry->nSig << "    ";
        cout << lpEntry->pfn << "\n";
        ++lpEntry;
    }*/
}
//------------------------------------------------------------------
// main
//------------------------------------------------------------------
void main()
{
    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;

    // test Message Routing
    cout << endl << "pMyFrame receive WM_CREATE, ";
    cout << "routing path and call stack:" << endl;
    AfxWndProc(0, WM_CREATE, 0, 0, pMyFrame);

    cout << endl << "pMyView receive WM_PAINT, ";
    cout << "routing path and call stack:" << endl;
    AfxWndProc(0, WM_PAINT, 0, 0, pMyView);

    cout << endl << "pMyView receive WM_COMMAND, ";
    cout << "routing path and call stack:" << endl;
    AfxWndProc(0, WM_COMMAND, 0, 0, pMyView);

    cout << endl << "pMyFrame receive WM_COMMAND, ";
    cout << "routing path and call stack:" << endl;
    AfxWndProc(0, WM_COMMAND, 0, 0, pMyFrame);
}

结果

这里写图片描述
这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/xiaolixi199311/article/details/81297073