How do the ATL / WTL alternative message cards (ALT_MSG_MAPs) work? When do I use them?

I read the documentation that says

ATL supports alternative message cards declared with the ALT_MSG_MAP macro.
Each alternate message card is identified by a unique number, which you go to ALT_MSG_MAP .
Using alternative message cards, you can process messages from multiple windows on one card.
Please note that by default, CWindowImpl does not use alternative message cards.
To add this support, override the WindowProc method in the CWindowImpl -derived class and call ProcessWindowMessage with the message map identifier.

And when I look at WTL, I see message cards like:

 BEGIN_MSG_MAP(CCommandBarCtrlImpl) MESSAGE_HANDLER(WM_CREATE, OnCreate) MESSAGE_HANDLER(WM_FORWARDMSG, OnForwardMsg) ... ALT_MSG_MAP(1) // Parent window messages MESSAGE_HANDLER(WM_INITMENUPOPUP, OnParentInitMenuPopup) ... ALT_MSG_MAP(2) // MDI client window messages // Use CMDICommandBarCtrl for MDI support ALT_MSG_MAP(3) // Message hook messages MESSAGE_HANDLER(WM_MOUSEMOVE, OnHookMouseMove) ... END_MSG_MAP() 

However, I do not understand:

  • How are they caused. (How does the code know about the existence of alternative message cards?)

  • How do they differ from default message cards. They all look as if they are processing the same messages for the same windows ...

  • Why are they useful? (Are they not all for one window?)

Does anyone have a better explanation of what alternative message cards do?
(Motivation for why they were invented will be very helpful.)

+4
source share
1 answer

An alternative message map allows you to define message handlers on one map for messages from other windows. Take a look at your map above:

 BEGIN_MSG_MAP(CCommandBarCtrlImpl) MESSAGE_HANDLER(WM_CREATE, OnCreate) MESSAGE_HANDLER(WM_FORWARDMSG, OnForwardMsg) ... ALT_MSG_MAP(1) // Parent window messages MESSAGE_HANDLER(WM_INITMENUPOPUP, OnParentInitMenuPopup) ... 

This is the CCommandBarCtrlImpl class, and its associated window handle, HWND , is associated with it. All messages go through the card by default (lines above ALT_MSG_MAP(1) ).

Not for some reason the class wants to process the parent's messages. It subclasses it with a member class variable, and you get to the point where you need to define message handlers. You cannot add them directly to the same card, because it will be a mess with your own window messages.

Alternative maps help here. Messages from the parent window are sent to alternative message card No. 1.

You do not need alternative maps if you only process messages from your window.

+5
source

All Articles