您的位置:首页 > 其它

MFC 消息循环仿真

2011-09-27 16:57 148 查看
************************************************** ****************head.h********************* ************************************************************

#include <iostream>

using namespace std;

//mfc.h begin

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

////////////////////////////////////////////////////////////////////

// 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;//this macro is put into the class

#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 } \

};

enum AfxSig

{

AfxSig_end = 0, // [marks end of message map]

AfxSig_vv,

};

#define ON_COMMAND(id, memberFxn) \

{ WM_COMMAND, 0, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)memberFxn },

// Message map signature values and macros in separate header

class CObject

{

public:

CObject::CObject() {

cout<<"CObject"<<endl;

}

CObject::~CObject() {

}

};

class CCmdTarget : public CObject

{

public:

CCmdTarget::CCmdTarget() {

cout<<"CCmdTarget"<<endl;

}

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() {

cout<<"CWinThread"<<endl;

}

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() {

cout<<"CWinApp "<<endl;

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() {

cout<<"CDocument"<<endl;

}

CDocument::~CDocument() {

}

virtual BOOL OnCmdMsg(UINT nID, int nCode);

DECLARE_MESSAGE_MAP()

};

class CWnd : public CCmdTarget

{

public:

CWnd::CWnd() {

cout<<"CWnd"<<endl;

}

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() {

cout<<"CFrameWnd"<<endl;

}

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() {

cout<<"CView"<<endl;

}

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);

// global function

//

////*********************************************mfc.h end*************************************************************

//***********************************************my.h begin***********************************************

class CMyWinApp : public CWinApp

{

public:

CMyWinApp::CMyWinApp() {

cout<<"CMyWinApp"<<endl;

}

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() {

cout<<"CMyDoc"<<endl;

}

CMyDoc::~CMyDoc() {

}

DECLARE_MESSAGE_MAP()

};

class CMyView : public CView

{

public:

CMyView::CMyView() {

cout<<" CMyView"<<endl;

}

CMyView::~CMyView() {

}

DECLARE_MESSAGE_MAP()

};

//*********************************************my.h end****************************************************

//*********************************************mfc.cpp begin*****************************************************

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()" << 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;

}

//*********************************************mfc.cpp end****************************************************

******************************************************head.h is over*****************************************************

******************************************************cpp is forward*********************************************************

CMyWinApp theApp;

//------------------------------------------------------------------

// main

//------------------------------------------------------------------

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;

}

}

}

//------------------------------------------------------------------

// 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);

cout<<endl;

}

//------------------------------------------------------------------

什么都不说了 F10吧 ~~~~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: