How Do I Use a Single Message-Response Function To Respond to Different Command Messages?

Back Up Next

Sometimes, a series of command messages may be related in such a way that it makes more sense to handle them all with a single message-response func-tion than to create a message-response function for each. For example, a Line menu may enable a user to select one of several line thicknesses. It's logical to handle all Line menu commands in a single function whose job it is to set the line width. To perform this trick, you need to use EV_COMMAND_AND_ID macros in your window class's response table.

The Solution

First, create a response table that contains an EV_COMMAND_AND_ID macro for each message you want handled by the single message-response function, using the same function as the macro's second argument:

DEFINE_RESPONSE_TABLE1(TWndw, TFrameWindow)
  EV_COMMAND_AND_ID(CM_COHHAND1, CmCommand),
  EV_COMMAND_AND_ID(CM_COMMAND2, CmCommand),
  EV_COMMAND_AND_ID(CM_COMHAND3, CmCommand),
END RESPONSE TABLE;

The EV_COMMAND_AND_ID macro takes exactly the same arguments as the EV_COMMAND macro: the message ID and the name of the function that will handle that message.
The difference is that, when the program calls the message-response function, it sends along the ID of the message that triggered the call. In the message-response function, use this parameter to determine which message you need to respond to:

void TWndw::CmCommand(WPARAM messageld)
{
  switch (messageld) {
    case CM_COMMAND1 /* Handle message 1 */
      break;
    case CM_COMMAND2 /* Handle message 2 */
      break;
    case CM_COMMAND3 /* Handle message 3 */
      break;
  }
}


Copyright © 1998-2001 Yura Bidus. All rights reserved.