HOWTO: MFC user defined message maps

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

We will add the user defined message WM_FIND, identified by the string literal "MYMESSAGE", to a Document/View MFC application. First of all, the message can only be posted to objects of class CWnd or classes derived from CWnd, i.e. actual windows. This makes sense; only windows can receive window messages. Also note, that the class wizard CANNOT be used to introduce message maps for user defined messages. This task must be completed manually.


Add the prototype for the function to which the message is to be mapped. This function will be named "On" + (Second half of message name), here: "OnFind()". Further, the function declaration will be preceded by the term "afx_msg". We will define this function to be of type LRESULT (32 bit identifier) , and take the parameters (WPARAM wParam, LPARAM lParam), the standard window messaging parameters. The reason for this lies in the macro expansion of the AFX message map. If your function is not declared in compliance with the AFX message map macro, you will get error C2642.

Look for a section of code like this:

	// Generated message map functions
	afx_msg void OnFileConfig();

Add the following line to the above list of function declarations:
	afx_msg LRESULT OnFind(WPARAM wParam, LPARAM lParam);


Register the new window message. The string literal will later be used to retrieve the number allocated to WM_FIND.

Add the following line somewhere at the top of the file:

	int WM_FIND = RegisterWindowMessage ("MYMESSAGE");

Map the message onto a member function. The AFX message map entry must use the key word ON_REGISTERED_MESSAGE, and associate the message number with the function name.

Look for a section of code like this:


Add the following line to the above list of message maps:


Add a message handler function to the class. Replace "CMyView" with the class name of the view class in your application:

LRESULT CMyView::OnFind(WPARAM wParam, LPARAM lParam)
	// do something useful

	return (LRESULT) MyReturnValue;


Provide the document class that is to post the message with scope to the view class window handle.

Add a member to hold a window handle.

	HWND PostToView;


Obtain a reference to the document associated with the view and initialize the document's member variable (above) with the handle to the view's window.

Add the lines shown below to a member function that will be executed before the user message is posted to the view. Replace "CMyDoc" with the class name of your document class. This assignment cannot take place in the view's constructor. The OnCreate() member, however, would be a great place.

	"CMyDoc"* DocPtr = GetDocument();
	DocPtr->PostToView = this->m_hWnd;


Where you want to post the message, retrieve the reference to WM_FIND, the user defined message and then post the message...

A second call to RegisterWindowMessage() with the same string literal as an argument will retrieve the previously assigned ID for WM_FIND.

Add the code shown below in a function member of the document class from which you wish to post a message. Substitute the question marks for your actual parameters.

	int WM_FIND = RegisterWindowMessage ("MYMESSAGE");

	PostMessage(PostToView,   // handle to view window
	WM_FIND,                  // message to post
	(WPARAM) "?",             // window message parameter
	(LPARAM) "?");            // window message parameter

Alternatively, you can use SendMessage(). In contrast to PostMessage, which sends the message `spiraling' through the Windows messaging system and returns control to the calling thread before the message is actually processed, SendMessage will invoke the target message handler before returning execution control to the caller of SendMessage. The first is appropriate for asynchronous, de-coupled processing, the latter for synchronous processing.


  • Problem Posting RegisteredMessages

    Posted by Legacy on 11/29/2003 12:00am

    Originally posted by: David

    I have a CDialog Based Application, where I use a RegisteredMessage to comunicate it with a thread, if I
    post the message from the thread using one XPropertyPage
    Library, nothing is sent but I get no error from PostMessage, if I post the message without using XPropertyPage Library, everything works fine, any idea of what is happenning?

    thanks in advance.

  • Use ::PostMessage for PocketPC

    Posted by Legacy on 10/26/2001 12:00am

    Originally posted by: John Jorsett

    Windows CE 3.0 has two forms of PostMessage. To get the one that accepts a window handle argument, use ::PostMessage.

  • Thanks this stopped me tearing my own hair out

    Posted by Legacy on 02/10/2001 12:00am

    Originally posted by: Simon Aldrich

    Just to let you know, this worked a treat for me, sending messages between MFC dialogs in the main app and ancillary dlls.

    Thanks so much.


  • dest . windows handle

    Posted by Legacy on 02/15/2000 12:00am

    Originally posted by: chengchun

    you can SendMessage((HWND)-1,WM_find,wparam,lparam))

  • is there any difference between non-MFC and MFC messaging?

    Posted by Legacy on 09/20/1999 12:00am

    Originally posted by: odessa

    my way of using message in MFC framework is different in some point. my method is based on classical user-defined message passing, but yours is something structural, i think.
    i wonder if there is any difference between non-MFC message and MFC messaging.
    my question is why should we use ON_REGISTERED_MESSAGE macro? i use ON_MESSAGE(WM_MY_MESSAGE, OnMyHandler) instead of it. is there any reason for that?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The software-defined data center (SDDC) and new trends in cloud and virtualization bring increased agility, automation, and intelligent services and management to all areas of the data center. Businesses can now more easily manage the entire lifecycle of their applications and services via the SDDC. This Aberdeen analyst report examines how a strong foundation in both the cloud and internal data centers is empowering organizations to fully leverage their IT infrastructure and is also preparing them to be able …

  • This case study will highlight the opportunity for mobile and cloud technologies in the Public Sector—specifically for state, local and federal government agencies -where secure mobile application access can offer key productivity gains

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date