现在的位置: 首页 > 综合 > 正文

手写MFC—-第四讲 BEGIN_MESSAGE_MAP

2013年10月30日 ⁄ 综合 ⁄ 共 3703字 ⁄ 字号 评论关闭

首先看定义

#define
BEGIN_MESSAGE_MAP(theClass, baseClass) /

        
const AFX_MSGMAP* theClass::GetMessageMap()
const /

        
         {
return &theClass::messageMap; } /

        
AFX_COMDAT AFX_DATADEF
const AFX_MSGMAP theClass::messageMap = /

        
{ &baseClass::messageMap, &theClass::_messageEntries[0] }; /

        
AFX_COMDAT
const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = /

        
{ /

实际应用BEGIN_MESSAGE_MAP(myview,CView)等价于

        
const AFX_MSGMAP* myview::GetMessageMap()
const /

        
         {
return & myview::messageMap; } /

        
AFX_COMDAT AFX_DATADEF
const AFX_MSGMAP myview::messageMap = /

        
{ & CView::messageMap, & myview::_messageEntries[0] }; /

        
AFX_COMDAT
const AFX_MSGMAP_ENTRY myview::_messageEntries[] = /

        
{ /

还有END_MESSAGE_MAPBEGIN_MESSAGE_MAP是成对出现的

#define
END_MESSAGE_MAP() /

                  
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } /

        
}; /

整个程序改成如下:

#include
<afxwin.h>

#include
<afxext.h>

#include
"resource.h"

 

class mydoc:public
CDocument

{

public:

                  
        
static
const CRuntimeClass 
classmydoc;

                  
        
virtual CRuntimeClass* GetRuntimeClass()
const;

                  
        
static CObject* 
__stdcall 
CreateObject();

                  

public :

        
CString s;

        
CPoint point;

        
mydoc()

        
{

        
         s="Welcome to SDI";

        
         point.x=250;

        
         point.y=100;

        
}

};

 

CObject*
__stdcall mydoc::CreateObject()

{
return
new mydoc; }

const 
CRuntimeClass mydoc::classmydoc =

{

"mydoc",
sizeof(class mydoc), 0xFFFF,
mydoc::CreateObject,

(CRuntimeClass*)(& CDocument::classCDocument), NULL };

        
CRuntimeClass* mydoc::GetRuntimeClass()
const

{
return ((CRuntimeClass*)(& mydoc::classmydoc)); }

 

 

class myview:public
CView

{

public:

        
static
const CRuntimeClass 
classmyview;

        
virtual CRuntimeClass* GetRuntimeClass()
const;

        
static CObject* 
__stdcall 
CreateObject();

public:

        
void OnDraw(CDC *d)

        
{

                  
mydoc *b;

        
         b=(mydoc *)m_pDocument;

        
         d->SetTextColor(RGB(0,0,0)) ;

        
         d->TextOut(b->point.x,b->point.y ,b->s) ;

                  

        
}

        

        
void OnLButtonDown(UINT nFlags,CPoint p)

        
{

                  
mydoc *b;

        
         b=(mydoc*)m_pDocument;

        
         b->point.x=p.x;

        
         b->point.y=p.y;

        
         Invalidate();

        
}

        

private:

                           
 
static
const AFX_MSGMAP_ENTRY _messageEntries[];

protected:

                  
  
static AFX_DATA
const AFX_MSGMAP messageMap;

                  
  
virtual
const AFX_MSGMAP* GetMessageMap()
const;

                  
  

                  

};

 

CObject*
__stdcall myview::CreateObject()

{
return
new myview; }

 

const 
CRuntimeClass myview::classmyview =

{

        
"myview",
sizeof(class
myview), 0xFFFF, myview::CreateObject,

        
         (CRuntimeClass*)(& CView::classCView), NULL

};

CRuntimeClass* myview::GetRuntimeClass()
const

{

        
return ((CRuntimeClass*)(& myview::classmyview));

}

 

const AFX_MSGMAP* myview::GetMessageMap()
const

{
return & myview::messageMap; }

 

 const
AFX_MSGMAP myview::messageMap =

{ & CView::messageMap, & myview::_messageEntries[0] };

 

 const
AFX_MSGMAP_ENTRY myview::_messageEntries[] =

{

 

        
{

        
WM_LBUTTONDOWN, 0, 0, 0, AfxSig_vwp,

        
(AFX_PMSG)(AFX_PMSGW)(
void (CWnd::*)(UINT, CPoint))&OnLButtonDown

        
},

                  

 
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }

 

};

 

class zzz:public
CWinApp

{

public:

        
int InitInstance()

        
{

        
         CSingleDocTemplate *y;

        
         CRuntimeClass *w,*d,*v;

        
         d=(CRuntimeClass*)(&mydoc::classmydoc);

        
         w=(CRuntimeClass*)(&CFrameWnd::classCFrameWnd);

        
         v=(CRuntimeClass*)(&myview::classmyview);

                  
y=
new CSingleDocTemplate(IDR_MENU1,d,w,v);

        
         AddDocTemplate(y);

        
         OnFileNew();

                  
return 1;

        
}

};

 

zzz a;

 (全文完)

抱歉!评论已关闭.