首先,先向各位道歉,小的不才,不是候老师没办法。近日看了候老师的《深入浅出MFC》
   在消息传递那章卡住了,百思不得其解,忘有能者相助。自当重谢。
   问题是,为什么这些代码编译不过?
//mfc.cpp ************************************************************
#include "my.h"  // ­ì¸Ó§t¤J mfc.h ´N¦n¡A¦ý¬°¤F extern CMyWinApp ©Ò¥H...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&iexcl;A 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()" << 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()" << endl;
    if (CCmdTarget::OnCmdMsg(nID, nCode))
        return TRUE;    return FALSE;
}BOOL CView::OnCmdMsg(UINT nID, int nCode)
{
    cout << "CView::OnCmdMsg()" << 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;
}

解决方案 »

  1.   

    //mfc.h**************************************************
    #define TRUE 1
    #define FALSE 0typedef 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 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.h>////////////////////////////////////////////////////////////////////
    // Window message map handlingstruct 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 onlyclass 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 viewpublic:
      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);
      

  2.   

    //AFXMSG.h*****************************************
    enum AfxSig
    {
            AfxSig_end = 0,     // [s end of message map]
            AfxSig_vv,
    };#define ON_COMMAND(id, memberFxn)    { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)memberFxn },
    //my.cpp***************************************************
    #include "my.h"CMyWinApp theApp;  // global objectBOOL 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;
            }
        }
    }
    //------------------------------------------------------------------
    // 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);
    }//my.h*****************************************************
    #include <iostream.h>
    #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()
    };
      

  3.   

    我的ggjjmmdd啊,你把编译报的错贴出来就可以了,这么多代码你要人命啊
      

  4.   

    先看错误,再看代码,在什么方式下编译的?Console?
      

  5.   

    hehe.
    gz.
    樓主 提問也是有學問的.要多養成好習慣.
    比如說你的錯誤:
    既然是編譯錯誤,那就把錯誤鐵出來,而且響應的代碼也鐵出來,大家再給你想法子.....