본문 바로가기

공부/MFC

BEGIN_MESSAGE_MAP

BEGIN_MESSAGE_MAP와 END_MESSAGE_MAP




Begins the definition of your message map.

 
BEGIN_MESSAGE_MAP(theClass, baseClass )

Parameters

theClass

Specifies the name of the class whose message map this is.

baseClass

Specifies the name of the base class of theClass.

Remarks

In the implementation (.cpp) file that defines the member functions for your class, start the message map with the BEGIN_MESSAGE_MAP macro, then add macro entries for each of your message-handler functions, and complete the message map with the END_MESSAGE_MAP macro.

Example
 
// Example for BEGIN_MESSAGE_MAP
BEGIN_MESSAGE_MAP( CMyWindow, CFrameWnd )
    ON_WM_PAINT()
    ON_COMMAND( IDM_ABOUT, OnAbout )
END_MESSAGE_MAP( )
 
설명하자면 메시지를 처리하는 함수 테이블을 정의 하는 매크로로
BEGIN_MESSAGE_MAP로 시작하여 END_MESSAGE_MAP로 끝나도록 정의하고
메시지가 전달되면 호출할 메시지 핸들러 함수 테이블을 정의하는 역할을 합니다.
 
전체적으로 구성을 설명하면
.h에
 
class CMyDlg : public CDialog
{
   ...
protected:
 // 생성된 메시지 맵 함수
 afx_msg void OnPaint();
 DECLARE_MESSAGE_MAP()
};
 
다음과 같이 선언되면 DECLARE_MESSAGE_MAP 매크로로 인해
 
#define DECLARE_MESSAGE_MAP() \
protected: \
 static const AFX_MSGMAP* PASCAL GetThisMessageMap(); \
 virtual const AFX_MSGMAP* GetMessageMap() const; \
 
클래스에는 GetThisMessageMap이라는 정적 멤버함수와 GetMessageMap() 멤버함수가 선언됩니다.
 
 
.cpp에는
BEGIN_MESSAGE_MAP과 END_MESSAGE_MAP은 각각 다음과 같이 정의되어 있는데
 
#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
 PTM_WARNING_DISABLE \
 const AFX_MSGMAP* theClass::GetMessageMap() const \
  { return GetThisMessageMap(); } \
 const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() \
 { \
  typedef theClass ThisClass;         \
  typedef baseClass TheBaseClass;        \
  static const AFX_MSGMAP_ENTRY _messageEntries[] =  \
  {
 
#define END_MESSAGE_MAP() \
  {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
 }; \
  static const AFX_MSGMAP messageMap = \
  { &TheBaseClass::GetThisMessageMap, &_messageEntries[0] }; \
  return &messageMap; \
 }          \
 PTM_WARNING_RESTORE
 
 
BEGIN_MESSAGE_MAP은 GetMessageMap의 구현과
GetThisMessageMap의 함수를 시작하여
END_MESSAGE_MAP까지 AFX_MSGMAP_ENTRY라는 구조체를 이용하여
메시지 ID와 핸들러 함수의 관계를 나타내는 테이블을 구성합니다.
 
윈도우의 메시지가 발생할경우 메시지핸들러는 GetThisMessageMap을 통해
메시지 ID를 검사하여 핸들러 함수를 찾아 호출해줍니다.
 
#define ON_WM_PAINT() \
 { WM_PAINT, 0, 0, 0, AfxSig_vv, \
  (AFX_PMSG)(AFX_PMSGW) \
  (static_cast< void (AFX_MSG_CALL CWnd::*)(void) > ( &ThisClass :: OnPaint)) },
 
예를 들어 WM_PAINT라는 메시지가 윈도우에 전달되면 OnPaint를 호출해줍니다.



-- 출처 : 네이버 지식인 검색 --  링크