MSDN中给出的“消息映射”宏的说明
宏名:DECLARE_MESSAGE_MAP
使用方式:DECLARE_MESSAGE_MAP( )
Remarks
Each CCmdTarget-derived class in your program must provide a message map to handle messages.
Use the DECLARE_MESSAGE_MAP macro at the end of your class declaration.
Then, in the .CPP file that defines the member functions for the class, use the BEGIN_MESSAGE_MAP
macro, macro entries for each of your message-handler functions, and the END_MESSAGE_MAP macro.
Note If you declare any memberafter DECLARE_MESSAGE_MAP, you must specify a new access type
(public, private, or protected) for them.
For more information on message maps and the DECLARE_MESSAGE_MAP macro,
see Message Handling and Mapping Topics in Visual C++ Programmer's Guide.
Example
// example for DECLARE_MESSAGE_MAP
class CMyWnd : public CFrameWnd
{
// Member declarations
DECLARE_MESSAGE_MAP( )
};
---------------------------------------------------------
BEGIN_MESSAGE_MAP
BEGIN_MESSAGE_MAP( theClass, baseClass )
Parameters
theClass //指定:这是哪个类的消息映射
Specifies the name of the class whose message map this is.
baseClass //指定:要进行消息映射的这个类的父类是谁
Specifies the name of the base class of theClass.
Remarks
Use the BEGIN_MESSAGE_MAP macro to begin the definition of your message map.
In the implementation (.CPP) file that defines the member functions for your class,
start the message map with the BEGIN_MESSAGE_MAP macro, then add macro entries for
each of your message-handler functions, and complete the message map with the
END_MESSAGE_MAP macro.For more information on message maps and the BEGIN_MESSAGE_MAP macro,
see Adding a Dialog Box in Visual C++ Tutorials.
Example
// example for BEGIN_MESSAGE_MAP
BEGIN_MESSAGE_MAP( CMyWindow, CFrameWnd )
//{{AFX_MSG_MAP( CMyWindow )
ON_WM_PAINT()
ON_COMMAND( IDM_ABOUT, OnAbout )
//}}AFX_MSG_MAP
END_MESSAGE_MAP( )
--------------------------------------------------------------------
END_MESSAGE_MAP
END_MESSAGE_MAP( )
Remarks
Use the END_MESSAGE_MAP macro to end the definition of your message map.
For more information on message maps and the END_MESSAGE_MAP macro,
see Message Handling and Mapping Topics in Visual C++ Programmer's Guide.
--------------------------------------------------------------
Message Maps
This section of the reference lists all message mapping macros and all CWnd
message-map entries along with the corresponding member function prototypes:
Category Description WM_COMMAND Message Handler Handles WM_COMMAND messages generated by user menu selections or menu access keys. Child Window Notification Message Handlers Handle notification messages from child windows. WM_ Message Handlers Handle WM_ messages, such as WM_PAINT. User-Defined Message Handlers Handle user-defined messages.
(For an explanation of the terminology and conventions used in this reference,
see How to Use the Message Map Cross-Reference.)
Since Windows is a message-oriented operating system, a large portion of programming for
the Windows environment involves message handling. Each time an event such as a keystroke
or mouse click occurs, a message is sent to the application, which must then handle the
event.
The Microsoft Foundation Class Library offers a programming model optimized for
message-based programming. In this model, "message maps" are used to designate which
functions will handle various messages for a particular class. Message maps contain one
or more macros that specify which messages will be handled by which functions. For example,
a message map containing an ON_COMMAND macro might look something like this:
BEGIN_MESSAGE_MAP( CMyDoc, CDocument )
//{{AFX_MSG_MAP( CMyDoc )
ON_COMMAND( ID_MYCMD, OnMyCommand ) // ... More entries to handle additional commands
//}}AFX_MSG_MAP
END_MESSAGE_MAP( )
The ON_COMMAND macro is used to handle command messages generated by menus, buttons,
and accelerator keys. Macros are available to map the following:
Windows Messages
- Control notifications
- User-defined messages
Command Messages
- Registered user-defined messages
- User-interface update messages
Ranges of Messages
- Commands
- Update handler messages
- Control notifications
Although message-map macros are important, you generally won't have to use them directly.
This is because ClassWizard automatically creates message-map entries in your source files
when you use it to associate message-handling functions with messages. Any time you want
to edit or add a message-map entry, you can use ClassWizard.
Note ClassWizard does not support message-map ranges. You must write these message-map
entries yourself.
However, message maps are an important part of the Microsoft Foundation
Class Library. You should understand what they do, and documentation is provided for them.
浙公网安备 33010602011771号