I searched but I didn't found. I want a Message map macro that links the menu item command action to a function accepting the numeric ID and providing no return at all (being void).
ON_COMMAND
returns void, but it is too limited, because it does not provide the menu item ID, which I need in this case.
ON_COMMAND_EX
returns a BOOL
, so it forces me to do a lot of returns, which would be unneeded if the return type was void.
So, as I described in first paragraph, is there such a macro?
I didn't found such a macro but I came with a solution: Define my own macro based on the definition of ON_COMMAND_EX
, replacing BOOL
by void
.
The ON_COMMAND_EX
macro is:
#define ON_COMMAND_EX(id, memberFxn) \
{ WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSigCmd_EX, \
(AFX_PMSG) \
(static_cast< BOOL (AFX_MSG_CALL CCmdTarget::*)(UINT) > \
(memberFxn)) },
I've just copied and adapted it to my own purposes:
#define ON_COMMAND_EX_VOID(id, memberFxn) \
{ WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSigCmd_EX, \
(AFX_PMSG) \
(static_cast< void (AFX_MSG_CALL CCmdTarget::*)(UINT) > \
(memberFxn)) },
Notice the only two changes are the name of tha macro and change from BOOL
to void
To use it: On the message map, add something like
ON_COMMAND_EX_VOID(ID_FILE_PREFERENCES, OnFilePreferencesVoid)
Then declare the handler function on the header file: afx_msg void OnFilePreferencesVoid(UINT nID);
And finally do the implementation on the source code file:
void CMainFrame::OnFilePreferencesVoid(UINT nID)
{
CString s;
s.Format(_T("%d"), nID);
AfxMessageBox(s);
}
Obviously, the code posted here is a theoretical example, as there are more useful things to do than display an already annoying popup message with an irritant resource ID.
The inspiration for the solution came from ON_MESSAGE_VOID
posted on https://stackoverflow.com/a/10619963/383779