I was just wondering what (if any) the difference was between the following two message traps in MFC for the function, OnSize(..).
1 – Via Message map:
BEGIN_MESSAGE_MAP(CClassWnd, CBaseClassWnd) ... ON_WM_SIZE() .. END_MESSAGE_MAP()
2 – Via afx_message:
afx_msg type OnSize(...);
They seem to be used interchangeably, which one should be used or does it depend on other factors?
Both parts are necessary to add a message handler to a class. The message map should be declared inside your class, together with declarations for any message handler functions (e.g,
OnSize).afx_msgis just an empty placeholder macro – it doesn’t actually do anything, but is always included by convention.The message map is then defined in the class’s .cpp file:
These macros generate a lookup table for the class which allows messages received by the window to be dispatched to the corresponding handler functions. The
ON_WM_SIZEmacro allows thewParamandlParammessage parameters in theWM_SIZEmessage to be decoded into more meaningful values for the message handler function (nType,cx, andcyin this case). MFC provides macros for most window messages (WM_LBUTTONDOWN,WM_DESTROY, etc).You can find more information on how message maps work in MFC here on MSDN.