Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 5: The Document/View Architecture Visual C++ and MFC Fundamentals


4.1 Introduction to Messages............................................................................


4.1.1 Overview...............................................................................................


Some of your applications will be made of various objects. Most of the time, more than
one application is running on the computer. These two scenarios mean that the operating
system is constantly asked to perform some assignments. Because there can be so many
requests presented unpredictably, the operating system leaves it up to the objects to
specify what they want, when they want it, and what behavior or result they expect.

The Microsoft Windows operating system cannot predict what kinds of requests one
object would need to be taken care of and what type of assignment another object would
need. To manage all these assignments and requests, the objects send messages, one
message at a time, to the operating system. For this reason, Microsoft Windows is said to
be a message-driven operating system.

The messages are divided in various categories but as mentioned already, each object has
the responsibility to decided what message to send and when. Therefore, most of the
messages we will review here are part of a window frame. Others will be addressed when
necessary.

Once a control has composed a message, it must send it to the right target which could be
the operating system. In order to send a message, a control must create an event. It is also
said to fire an event. To make a distinction between the two, a message's name usually
starts with WM_ which stands for Window Message. The name of an event usually starts
with On which indicates an action. Remember, the message is what needs to be sent. The
event is the action of sending the message.

4.1.2 A Map of Messages.............................................................................


For the compiler to manage messages, they should be included in the class definition. The
list of messages starts on a section driven by, but that ends with, the
DECLARE_MESSAGE_MAP macro. The section can be created as follows:

#include <afxwin.h>

class CSimpleFrame : public CFrameWnd
{
public:
CSimpleFrame();

DECLARE_MESSAGE_MAP()
};

The DECLARE_MESSAGE_MAP macro should be provided at the end of the class
definition. The actual messages (as we will review them shortly) should be listed just
above the DECLARE_MESSAGE_MAP line. This is just a rule. In some
circumstances, and for any reason, you may want, or have, to provide one message or a
few messages under the DECLARE_MESSAGE_MAP line.
Free download pdf