Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 5: The Document/View Architecture


The hWnd argument is the object or control that is sending the message.
The Msg argument is the message to be sent.
The wParam and the lParam values depend on the message that is being sent.

4.6.2 Sending Messages................................................................................


The advantage of using the SendMessage() function is that, when sending this message,
it would target the procedure that can perform the task and this function would return
only after its message has been processed. Because this (member) function can
sometimes universally be used, that is by any control or object, the application cannot
predict the type of message that SendMessage() is carrying. Therefore, (the probable
disadvantage is that) you must know the (name or identity of the) message you are
sending and you must provide accurate accompanying items (like sending a letter with
the right stamp; imagine you send a sexy letter to your grand-mother in Australia about
her already dead grand grand-father who is celebrating his first job while he has just
become 5 years old).

In order to send a message using the SendMessage() function, you must know what
message you are sending and what that message needs in order to be complete. For
example, to change the caption of a window at any time, you can use the
WM_SETTEXT message. The syntax to use would be:

SendMessage(WM_SETTEXT, wParam, lParam);

Obviously you would need to provide the text for the caption you are trying to change.
This string is carried by the lParam argument as a null-terminated string. For this
message, the wParam is ignored.

Practical Learning: Sending Messages



  1. To process a message using the SendMessage() function, change the OnRButtonUp()
    event as follows:


void CMainFrame::OnRButtonUp(UINT nFlags, CPoint point)
{
const char *Msg = "This message was sent";
SendMessage(WM_SETTEXT, 0, (LPARAM)(LPCTSTR)Msg);
}


  1. Test the application and return to MSVC

Free download pdf