Click here to Skip to main content
Licence 
First Posted 3 May 2001
Views 99,877
Bookmarked 26 times

New type-safe message maps in MFC

By | 15 Oct 2001 | Article
The example demonstrates the stricter type checking for the return and parameter types of message handler functions.

Introduction

The MsgMapDemo example demonstrates the changes that were made in MFC Message Map implementation. Beginning with Visual C++.NET, MFC provides stricter type checking for the return and parameter types of message handler functions. This new behavior notifies the developer of potential problems by flagging potentially unsafe message handlers with an error message. MFC now uses static casts for ON_MESSAGE, ON_REGISTERED_MESSAGE, ON_THREAD_MESSAGE, and ON_REGISTERED_THREAD_MESSAGE.

The example was created using the MFC Application AppWizard to create a dialog based application. The MsgMapDemo application shows a dialog with a button and vertical progress bar. A user defined message WM_USER_MESSAGE is used to move a progress indicator. It should be sent or posted to the dialog.

Creating the Demo app

To add a user defined message and handler make the following changes in the source code:

  1. A ON_MESSAGE(message, memberFxn) macro should be added to the dialog message map, where message is the message ID, and memberFxn is the name of the message-handler function to which the message is mapped.
    ...
    BEGIN_MESSAGE_MAP(CMsgMapdemoDlg, CDialog)
    	ON_WM_SYSCOMMAND()
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_BN_CLICKED(ID_BTN_OK, OnBtnClicked)
    	//}}AFX_MSG_MAP
    	ON_MESSAGE(WM_USER_MESSAGE, OnUserMessage)
    END_MESSAGE_MAP()
    ...
    
  2. Define the memberFxn message handler (OnUserMessage) with the following signature
    afx_msg LRESULT memberFxn(WPARAM, LPARAM)

    in dialog header file:

    afx_msg LRESULT OnUserMessage(WPARAM wParam, LPARAM lParam);
  3. Write the implementation for this function in the source file.
    //*******************************************************************
    //	OnUserMessage
    //
    //		Handles WM_USER_MESSAGE user message.
    //		Advances the current position for a progress bar by the step.
    //*******************************************************************
    LRESULT CMsgMapdemoDlg::OnUserMessage(WPARAM wParam, LPARAM lParam)
    {
    	CWnd* pProgress;
    
    	// Get a pointer to the progress bar window.
    	pProgress = GetDlgItem(IDC_PROGRESS);
    
    	// Move progress bar indicator one step.
    	if(pProgress != NULL)
    		pProgress->SendMessage(PBM_STEPIT);
    
    	return 0;
    }
    

The main difference from previous versions of MFC is that now the compiler checks the message handler's function signature. For user message using the ON_MESSAGE macro it should be:

afx_msg LRESULT memberFxn(WPARAM, LPARAM)

Previous versions of the compiler did not make this check, so you could use a function that uses the wrong number of parameters or the wrong parameter types, or return an incorrect value type, such as:

// Try it in VC6 release build, it will crash.
afx_msg LRESULT OnUserMessage(WPARAM wParam);	

// Try it in VC6 release build, it will crash.
afx_msg LRESULT OnUserMessage();		

// Try it in VC6 release build, it will crash.
afx_msg void OnUserMessage();

The compiler did not previously catch this, and very often it even worked in debug build, but in most cases it crashed in release mode so it could be found only at the final stages of developing. Now, if you use the wrong function signature, you will receive a Compiler Error C2440. This allows you to detect the problem immediately and potentiall save you a lot of debugging time.

Let's return to our code and finish it.

When the "Fill the glass" button is presses a WM_USER_MESSAGE message is sent to the dialog. To add code the code that will handle the button click and send the message you need to add the following macro to the dialog's message map:

ON_BN_CLICKED(ID_BTN_OK, OnBtnClicked)

Add a prototype of the OnBtnClicked function to the dialog header file:

afx_msg void OnBtnClicked();

Implement this function in the dialog source file:

//*******************************************************************
//	OnBtnClicked
//
//		Handles pressing dialog button.
//*******************************************************************
void CMsgMapdemoDlg::OnBtnClicked() 
{
	// Send use defined message to move progress indicator.
	SendMessage(WM_USER_MESSAGE);
}

The button click handler is now hooked up to the button, and when your user clicks on the button a WM_USER_MESSAGE message is sent to the dialog, and the OnUserMessage handler will be called.

History

16 Oct 2001 - updated files for VS beta 2

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Igor Chouvalov



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to make it work in CWinApp? Pinmembertyjiang0:50 13 Apr '06  
BEGIN_MESSAGE_MAP(CMyApp,CWinApp)
ON_REGISTERED_MESSAGE( MyMSGID, OnMyRegisteredMessage )
END_MESSAGE_MAP()
 
Confused | :confused:
Questionmessge crack? Pinmemberwei15:21 21 Oct '02  
GeneralComile Error with old projects (VC6.0) PinsussJ. Goldmann21:29 21 Aug '02  
GeneralMissing workspace file PinmemberManfred Ramosch5:19 4 May '01  
GeneralRe: Missing workspace file PinmemberThomas Freudenberg6:17 4 May '01  
GeneralRe: Missing workspace file PinmemberManfred Ramosch7:42 4 May '01  
GeneralRe: Missing workspace file PinmemberThomas Freudenberg9:14 4 May '01  
GeneralRe: Missing workspace file PinmemberIgor Chouvalov7:44 4 May '01  
GeneralRe: Missing workspace file PinmemberManfred Ramosch8:13 4 May '01  
GeneralGood one!! PinmemberAnonymous4:58 4 May '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 16 Oct 2001
Article Copyright 2001 by Igor Chouvalov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid