How Do I Send My Own Windows Messages?

Back Up Next

Many command message constants you see are prefaced with a CM, which stands for Command Message. A command message is the type of message you define for a menu item. Other messages you handle frequently in Windows programs are those whose constants are prefaced with WM, which stands for Windows Message. Although these two types of messages can handle most of what goes on in your programs, there's a third type of message that simpli-fies the task of communicating between window objects. These are user-defined Windows messages.

Windows defines a constant, WM_USER, to represent the value at which you can start defining your own messages. Windows reserves all messages from 0 to WM_USER -1 for its own use. However, you can define any messages you like

between the values of WM_USER and WM_USER+0x7FFF. It's not necessary to know the value of WM_USER. In fact, Microsoft may very well redefine the current value assigned to WM_USER. All you need to know is that you should define user-defined Windows messages starting at WM_USER.

The Solution

To use a user-defined message in a program, first add an EV_MESSAGE macro for the message to the window class's response table:

DEFINE_RESPONSE_TABLE1(TWndw, TFrameWindow)
  EV_MESSAGE(PM_MYMESSAGE, PmMyMessage),
END_RESPONSE_TABLE;

The EV_MESSAGE macro takes as its two parameters the message ID and the name of the function that will handle the message. You add the EV_MESSAGE macro to your response table just as you would many other response-table macros, by typing the name of the macro and the macro's arguments enclosed in parentheses.

The next step is to write the function that will respond to your new Windows message. This function must accept TParam1 and TParam2 parameters and must return an TResult value:

TResult TWndw::PmMyMessage(TParam1 param1, TParam1 param2)
{
  MessageBox("Message Received", "Message");
  return 1;
}

Of course, your message-response function will probably do much more than the example. You can handle the message in your response function any way you like.

For your new message-response function to get called, however, you must send your user-defined message to the window. You can have a window send the message to itself by calling the SendMessage() or PostMessage() functions (inherited from TWindow):

SendMessage(PM_MYMESSAGE, param1, param2);

or

PostMessage(PM_MYMESSAGE, param1, param2);

If you want to send the message from one window to another, preface the function call with a pointer to the receiving window:

wndw->PostMessage(PM_MYMESSAGE, wParam, IParam);

In any case, the arguments in the function call are the message's ID and the accompanying TParam1 (word parameter) and TParam2 (long parameter) values for the message. The latter two arguments can be any word and long word values you like. After all, it's your message!

SendMessage() and PostMessage() tackle the same job, but do it a little differ-ently. SendMessage() sends the message directly to the window and doesn't return until the message has been processed. PostMessage(), on the other hand, places the message in the receiving window's message queue and returns immediately. In other words, it may take longer for a message sent by PostMessage() to be processed. Note that both of these function calls are OWL encapsulated versions of Windows API functions of the same name.

You can also use PostMessage() and SendMessage() to send regular Windows messages. For example, to close a window, you could use the function call PostMessage(WM_CLOSE, 0, 0).

 


Copyright © 1998-2001 Yura Bidus. All rights reserved.