Message Passing

This document shows how to construct messages that your child windows will pass back to parents.

Our example is trivial, but serves as a frame work to build more complex message passing paradigms. The Child class of the accompanying program has a menu that calls a dialog box with two radio buttons. These buttons allow the user the choice of recieving data or sending a message. For various reasons the Client class will do the actual work, so the selection must be passed to the Client. The Child sends its message using the usual

parent->SendMessage(WM_OURMESSAGE);

Because the message being sent is not one of the standard windows messages, we have to define the message (WM_OURMESSAGE) as an identifier in the resource editor. When you define a new idenifier you must assign it a value. For messages I generally use values between 19000 and 20000. Care must be taken not to overlap your value with a built-in message or you will get unexpected results from standard operations.

Once you have defined your message identifiers (in this case WM_ASK and WM_SEND), you can create your dialog box with two buttons using Class Expert to transfer from you program to the Resource Workshop. Now you can use the usual procedure for creating the class corresponding to the dialog box.

void MesspassMDIChild::cmGetDialog ()
{
// INSERT>> Your code here.
	SendMessageDlgXfer tb; tb.
	_ask = true; tb.
	_send = false;
	if (SendMessageDlg(&tb,this).Execute()==IDOK)
	{
		if(tb._ask) 
			Parent->PostMessage(WM_ASK);
		else
			Parent->PostMessage(WM_SEND); }
} 

In the Client (parent of Child) we must associate the user defined messages with event handlers. We do this by appending the Response table with two EV_MESSAGEs.

// Build a response table for all messages/commands handled
// by MesspassMDIClient derived from TMDIClient.
//
DEFINE_RESPONSE_TABLE1(MesspassMDIClient, TMDIClient)
//{{MesspassMDIClientRSP_TBL_BEGIN}}
    EV_COMMAND(CM_FILEPRINT, CmFilePrint),
    EV_COMMAND(CM_FILEPRINTERSETUP, CmFilePrintSetup),
    EV_COMMAND(CM_FILEPRINTPREVIEW, CmFilePrintPreview),
    EV_COMMAND_ENABLE(CM_FILEPRINT, CmPrintEnable),
    EV_COMMAND_ENABLE(CM_FILEPRINTERSETUP, CmPrintEnable),
    EV_COMMAND_ENABLE(CM_FILEPRINTPREVIEW, CmPrintEnable),
    EV_WM_DROPFILES,
    EV_MESSAGE(WM_ASK, ask),
    EV_MESSAGE(WM_SEND, send),
//{{MesspassMDIClientRSP_TBL_END}}
END_RESPONSE_TABLE;

Finally we implement the event handlers.

LRESULT MesspassMDIClient::ask (WPARAM,LPARAM)
{
	MessageBox("Well you didn't really want to type anyway, did you?");
}
LRESULT MesspassMDIClient::send (WPARAM,LPARAM)
{
	MessageBox("Well what did you expect-The Declaration of Independence?");
}

For complete code download message.zip