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.

IN THE VIEW CLASS HEADER FILE

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
	protected:
	//{{AFX_MSG(CMyView)
	afx_msg void OnFileConfig();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()

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

IN THE VIEW CLASS BODY FILE

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:

	BEGIN_MESSAGE_MAP(CMyView, CListView)
	//{{AFX_MSG_MAP(CMyView)
	ON_COMMAND(ID_FILE_CONFIG, OnFileConfig)
	//}}AFX_MSG_MAP
	END_MESSAGE_MAP()

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

	ON_REGISTERED_MESSAGE(WM_FIND, OnFind)

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;
};

IN THE DOCUMENT CLASS HEADER FILE

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;

IN THE VIEW CLASS BODY FILE

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;

IN THE DOCUMENT CLASS BODY FILE

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.



Comments

  • 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.

    Reply
  • 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.

    Reply
  • 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.

    Simon

    Reply
  • 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))

    Reply
  • 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?


    Reply
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 …

  • Microsoft Azure® is a leading choice for businesses looking to take advantage of the cloud. Azure is particularly appealing to businesses that have already invested in Microsoft on-premises and are now considering running these applications and other workloads in the cloud. To understand how to make this move to Azure, many businesses are turning to managed service providers (MSPs) with specific Azure expertise. Read this white paper to learn the eight key areas to focus on when considering an MSP for an …

Most Popular Programming Stories

More for Developers

RSS Feeds

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