Solved

message map macro?

Posted on 2002-06-07
10
398 Views
Last Modified: 2008-03-03
What is the macro exactly? How is it different how a normal method/function/procedure? What C++ do with message map macro? Is it gonna be compiled to machine code or something?
0
Comment
Question by:quanghoc
  • 4
  • 3
  • 3
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 7063352
A message map macro is just a MFC construct to make it easier to connect windows messages to methods of a class - it basically is

#define DECLARE_MESSAGE_MAP() \
private: \
    static const AFX_MSGMAP_ENTRY _messageEntries[]; \
protected: \
    static AFX_DATA const AFX_MSGMAP messageMap; \
    static const AFX_MSGMAP* PASCAL _GetBaseMessageMap(); \
    virtual const AFX_MSGMAP* GetMessageMap() const; \

#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
    const AFX_MSGMAP* PASCAL theClass::_GetBaseMessageMap() \
        { return &baseClass::messageMap; } \
    const AFX_MSGMAP* theClass::GetMessageMap() const \
        { return &theClass::messageMap; } \
    AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass::messageMap = \
    { &theClass::_GetBaseMessageMap, &theClass::_messageEntries[0] }; \
    AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \
    { \

#define END_MESSAGE_MAP() \
        {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
    }; \

So, what it does is declaring an array of message entries and accessor methods for both the classe's map and the the map of the base class, which are evaluated when a message is routed through the framework...
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7063415
Don't get it!!?? So message map just create an array?
What about macro? What is that? Can I have my own macro? is message map macro is MFC's stuffs or C++'s stuffs?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7063428
>>So message map just create an array?

Basically, yes - but this array is pretty complex :o)

>>Can I have my own macro?

Sure - a macro in general is an expression that is expanded by the precompiler, e.g. like

#define ADD(a,b) a + b

int a = 1;
int b = 2;

int c = ADD(a,b);

will result in

int c = a + b;

>>is message map macro is MFC's stuffs or C++'s stuffs?

A message map is a MFC construct and certainly isn't included in any C++ standard.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 49

Expert Comment

by:DanRollins
ID: 7064161
>>Can I have my own macro?
You can add message-map entries to the map.  For instance:

  ON_MESSAGE( 123456, MyFn )


This means if a message comes in and MSG.message value is 123456, then the function maned MyFn will be executed.
 
-- Dan
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7064203
So, in what .h file I can find all the source that message map does? And to jkr: var c will be a global variable?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7064609
All the message map macros can be found in afxwin.h
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7064637
Why this has to done with macro? Why Microsoft doesn't just use a method to this? What is the advantage to use macro?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7064643
quanghoc,
Is this question about C++ macros in general?  Or is it about how MFC uses its message mapping system?  

Please describe what you need to know.  So far you are thrashing about without direction.

-- Dan
0
 
LVL 1

Author Comment

by:quanghoc
ID: 7064754
Well, as the begin questions:
-------------------
What is the macro exactly? How is it different how a normal method/function/procedure? What C++ do with
message map macro? Is it gonna be compiled to machine code
or something?
-------------------
So far, I already understand about message map and how macro construct. But as the last couple questions:
-------------------
Why this has to done with macro? Why Microsoft doesn't just use a method to this? What is the advantage
to use macro?
-------------------
I would like to know why they created macro for! That's it. The whole thing is just the relationship between macro and message map.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7064816
I think you want to know this:
Why did Microsoft use a set of macros to implement the message-handling table.

The reason is that the table is complicated and they wanted to provide an easy way for programmers and automated tools such as the the ClassWizard to add and remove items from the table.  

For instance, which would you rather type.  This...

BEGIN_MESSAGE_MAP(CD01Dlg, CDialog)
     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
     ON_NOTIFY(LVN_BEGINDRAG, IDC_LIST1, OnBegindragList1)
END_MESSAGE_MAP()

... or this ...

const AFX_MSGMAP* PASCAL theClass::_GetBaseMessageMap()
{ return &baseClass::messageMap; }
const AFX_MSGMAP* theClass::GetMessageMap() const
{ return &theClass::messageMap; }
AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass::messageMap =
 &theClass::_GetBaseMessageMap, &theClass::_messageEntries[0] };
AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] =
{
     { WM_LBUTTONDOWN, 0, 0, 0, AfxSig_vwp,
          (AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL CWnd::*)(UINT, CPoint))&OnLButtonDown
     },
     { WM_NOTIFY, (WORD)(int)wNotifyCode, (WORD)id,
          (WORD)id, AfxSig_vNMHDRpl,
          (AFX_PMSG)(void (AFX_MSG_CALL CCmdTarget::*)(NMHDR*, LRESULT*))&memberFxn
     },
     {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }
};

-- Dan
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now