EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

MFC Event handling

MFC uses very limited number of callback events via virtual functions. A wide range of events can be implemented by event message array. There is a static array of type AFX_MSGMAP_ENTRY named as _ messageEntries. AFX_MSGMAP_ENTRY is a structure which has all necessary members needed to represent a windows message.

Message Data Structure

struct AFX_MSGMAP_ENTRY
{
  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) */
};

MFC Message Map

User declares this array with DECLARE_MESSAGE_MAP and defines this with starting with BEGIN_MESSAGE_MAP and ends with END_MESSAGE_MAP. BEGIN_MESSAGE_MAP declares array name and opens static array entries. User defines event maps one by one. Then this array declaration is closed with END_MESSAGE_MAP which is an array element where all the member elements are zeros. MFC framework iterates through these array elements and calls the callback event function when a proper match is found. It stops if it reaches till the last element.

#define DECLARE_MESSAGE_MAP() \
private: \
  static const AFX_MSGMAP_ENTRY _messageEntries[]; \
protected: \
  static AFX_DATA const AFX_MSGMAP messageMap; \
  virtual const AFX_MSGMAP* GetMessageMap() const; \
#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
  const AFX_MSGMAP* theClass::GetMessageMap() const \
10      { return &theClass::messageMap; } \
11    AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass::messageMap = \
12    { &baseClass::messageMap, &theClass::_messageEntries[0] }; \
13    AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \
14    { \
15 
16  #define END_MESSAGE_MAP() \
17      {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)} \
18    }; \
19 
20  #define ON_WM_PAINT() \
21    { WM_PAINT, 0, 0, 0, AfxSig_vv, \
22      (AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL CWnd::*)(void))&OnPaint },
23 

Message Map Example

/* Use of MESSAGE_MAP */
class CMyDialog : public CDialog
{
  CMyDialog();
  void OnPaint();
  DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
10  ON_WM_PAINT()
11  END_MESSAGE_MAP()
"; format_source($code, strlen($code)); ?>

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sun 27 Apr/2014 03:53:39(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.