0
votes

I have a MDI MFC application, where I want to use SendMessage() from a CPropertieswnd class to invoke a function in CMainFrame class. The custom message is defined as:

#define WM_CUSTOM (WM_APP + 10)

The messagemap in Mainframe.cpp is :

ON_COMMAND(WM_CUSTOM , &CMainFrame::OnFileNewType1)    

In Propertieswnd.cpp file the message is sent:

AfxGetMainWnd()->SendMessage(WM_CUSTOM);

But the OnFileNewType1() function was never called. Can anyone please guide me which point I am missing ?

3

3 Answers

4
votes

You must use the ON_MESSAGE handler to process the message.

ON_MESSAGE

The function should be declared as follows inside CMainFrame:

afx_msg LRESULT OnFileNewType1(WPARAM wParam, LPARAM lParam);

1
votes

If you want to mimic the behavior of selecting a menu item or clicking a toolbar button and use the same code for menu item selection and sending your own commands, you can use the handler you already have. You just need to fix the arguments of your SendMessage.

AfxGetMainWnd()->SendMessage(WM_COMMAND, WM_CUSTOM, 0); 

All menu item clicks, accelerators and such are translated into WM_COMMAND messages, then routed through the system to call your ON_COMMAND handlers. If you want to piggyback on that system, you need to send WM_COMMAND messages as well. If not, you need to go the route of ON_MESSAGE as the others have suggested.

See here for more details on WM_COMMAND and it's wParam and lParam arguments.

1
votes

You require two steps to process the custom message you create. 1) Create a message handler function. 2) Map it with your custom message.

For Example.

#define WM_CUSTOM WM+10

Suppose your message handler function(part of your class) is as follows

afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);

Map your message and message handler in BEGIN_MESSAGE_MAP

BEGIN_MESSAGE_MAP(CMyWnd2, CWnd)
   ON_MESSAGE(WM_CUSTOM , OnMyMessage)
END_MESSAGE_MAP()

Now when

AfxGetMainWnd()->SendMessage(WM_CUSTOM);

is called. message handler function OnMyMessage will be called. You can write your code in this method.

Read more about the details of WPARAM and LPARAM so that you can utilize them as well.