Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Nonstandard Messages

Until now, the discussion has centered on regular Windows messages (those that begin with WM_XXX). However, two other major categories of messages merit some discussion: notification messages and user-defined messages.

Notification Messages

Notification messages are messages sent to a parent window when something happens in one of its child controls that might require the parent's attention. Notification messages occur only with the standard Windows controls (button, list box, combo box, and edit control) and with the Windows Common Controls (tree view, list view, and so on). For example, clicking or double-clicking a control, selecting some text in a control, and moving the scrollbar in a control all generate notification messages.

You can handle notification messages by writing message-handling procedures in the form that contains a particular control. Table 3.2 lists the Win32 notification messages for standard Windows controls.

Table 3.2 Standard Control Notification Messages



Button Notification



The user clicked a button.


A button is disabled.


The user double-clicked a button.


The user highlighted a button.


The button should be painted.


The highlight should be removed.

Combo Box Notification



The list box of a combo box has closed.


The user double-clicked a string.


The list box of a combo box is dropping down.


The user has changed text in the edit control.


Altered text is about to be displayed.


The combo box is out of memory.


The combo box is losing the input focus.


A new combo box list item is selected.




The user's selection should be canceled.


The user's selection is valid.


The combo box is receiving the input focus.

Edit Notification



The display is updated after text changes.


The edit control is out of memory.


The user clicked the horizontal scrollbar.


The edit control is losing the input focus.


The insertion is truncated.


The edit control is receiving the input focus.


The edit control is about to display altered text.


The user clicked the vertical scrollbar.

List Box Notification



The user double-clicked a string.


The list box is out of memory.


The list box is losing the input focus.


The selection is canceled.


The selection is about to change.


The list box is receiving the input focus.

Internal VCL Messages

VCL has an extensive collection of its own internal and notification messages. Although you don't commonly use these messages in your Delphi applications, Delphi component writers will find them useful. These messages begin with CM_ (for component message) or CN_ (for component notification), and they are used to manage VCL internals such as focus, color, visibility, window re-creation, dragging, and so on. You can find a complete list of these messages in the "Creating Custom Components" portion of the Delphi online help.

A common inquiry is how to detect that the mouse is entered or left a controls space. This can be handled by processing the custom messages CM_MOUSEENTER and CM_MOUSELEAVE. Consider the following component:

 TSpecialPanel = class(TPanel)
 procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER;
 procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;
procedure TSpecialPanel.CMMouseEnter(var Msg: TMessage);
 Color := clWhite;

procedure TSpecialPanel.CMMouseLeave(var Msg: TMessage);
 Color := clBtnFace; 

This component handles the custom messages by turning the panel white when the mouse has entered the component's surface area and then turns the color back to clBtnFace when the mouse leaves. You'll find an example of this code on the CD under the directory CustMessage.

User-Defined Messages

At some point, you'll come across a situation in which one of your own applications must send a message to itself, or you have to send messages between two of your own applications. At this point, one question that might come to mind is, "Why would I send myself a message instead of simply calling a procedure?" It's a good question, and there are actually several answers. First, messages give you polymorphism without requiring knowledge of the recipient's type. Messages are therefore as powerful as virtual methods but more flexible. Also, messages allow for optional handling: If the recipient doesn't do anything with the message, no harm is done. Finally, messages allow for broadcast notifications to multiple recipients and "parasitic" eavesdropping, which isn't easily done with procedures alone.

Messages Within Your Application

Having an application send a message to itself is easy. Just use the Perform(), SendMessage(), or PostMessage() function and use a message value in the range of WM_USER + 100 through $7FFF (the value Windows reserves for user-defined messages):


 SomeForm.Perform(SX_MYMESSAGE, 0, 0);
 { or }
 SendMessage(SomeForm.Handle, SX_MYMESSAGE, 0, 0);
 { or }
 PostMessage(SomeForm.Handle, SX_MYMESSAGE, 0, 0);

Then create a normal message-handling procedure for this message in the form in which you want to handle the message:

TForm1 = class(TForm)
 procedure SXMyMessage(var Msg: TMessage); message SX_MYMESSAGE;

procedure TForm1.SXMyMessage(var Msg: TMessage);
 MessageDlg('She turned me into a newt!', mtInformation, [mbOk], 0);

As you can see, there's little difference between using a user-defined message in your application and handling any standard Windows message. The real key here is to start at WM_USER + 100 for interapplication messages and to give each message a name that has something to do with its purpose.


Never send messages with values of WM_USER through $7FFF unless you're sure that the intended recipient is equipped to handle the message. Because each window can define these values independently, the potential for bad things to happen is great unless you keep careful tabs on which recipients you send WM_USER through $7FFF messages to.

Messaging Between Applications

When you want to send messages between two or more applications, it's usually best to use the RegisterWindowMessage() API function in each application. This method ensures that every application uses the same message number for a given message.

RegisterWindowMessage() accepts a null-terminated string as a parameter and returns a new message constant in the range of $C000 through $FFFF. This means that all you have to do is call RegisterWindowMessage() with the same string in each application between which you want to send messages; Windows returns the same message value for each application. The true benefit of RegisterWindowMessage() is that because a message value for any given string is guaranteed to be unique throughout the system, you can safely broadcast such messages to all windows with fewer harmful side effects. It can be a bit more work to handle this kind of message, though; because the message identifier isn't known until runtime, you can't use a standard message handler procedure, and you must override a control's WndProc() or DefaultHandler() method or subclass an existing window procedure. A technique for handling registered messages is demonstrated in Chapter 13, "Hard-Core Techniques," of Delphi 5 Developer's Guide, found on this book's CD-ROM. This useful demo shows how to prevent multiple copies of your application from being launched.


The number returned by RegisterWindowMessage() varies between Windows sessions and can't be determined until runtime.

Broadcasting Messages

TWinControl descendants can broadcast a message record to each of their owned controls—thanks to the Broadcast() method. This technique is useful when you need to send the same message to a group of components. For example, to send a user-defined message called um_Foo to all of Panel1's owned controls, use the following code:

 M: TMessage;
 with M do
 Message := UM_FOO;
 wParam := 0;
 lParam := 0;
 Result := 0;
  • + Share This
  • 🔖 Save To Your Account