Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
MFC
TRY
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

ON_MESSAGE

Indicates which function will handle a user-defined message.

ON_MESSAGE(message, memberFxn)

message

The message ID.

memberFxn

The name of the message-handler function to which the message is mapped.

The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM).

User-defined messages are any messages that are not standard Windows WM_MESSAGE messages. When selecting a message ID, you must use values within the range of WM_USER (0x0400) to 0x7FFF or WM_APP (0x8000) to 0xBFFF. For more information regarding message IDs, see WM_APP.

There should be exactly one ON_MESSAGE macro statement in your message map for every user-defined message that must be mapped to a message-handler function.

NoteNote:

In addition to user-defined messages, ON_MESSAGE handles less common Windows messages. For more information, see the Knowledge Base article Q99848.

For more information and examples, see Message Handling and Mapping Topics and User-Defined Handlers

#define WM_MYMESSAGE (WM_USER + 100)
BEGIN_MESSAGE_MAP(CMyWnd2, CWnd)
   ON_MESSAGE(WM_MYMESSAGE, OnMyMessage)
END_MESSAGE_MAP()
// inside the class declaration
 afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);
LRESULT CMyWnd2::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
   UNREFERENCED_PARAMETER(wParam);
   UNREFERENCED_PARAMETER(lParam);

   // Handle message here. 

   return 0;
}

Header: afxmsg_.h

Date

History

Reason

Corrected valid message ID ranges

Customer feedback.

Community Additions

ADD
Show:
© 2015 Microsoft