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
Notification |
Meaning |
Button Notification |
|
BN_CLICKED |
The user clicked a button. |
BN_DISABLE |
A button is disabled. |
BN_DOUBLECLICKED |
The user double-clicked a button. |
BN_HILITE |
The user highlighted a button. |
BN_PAINT |
The button should be painted. |
BN_UNHILITE |
The highlight should be removed. |
Combo Box Notification |
|
CBN_CLOSEUP |
The list box of a combo box has closed. |
CBN_DBLCLK |
The user double-clicked a string. |
CBN_DROPDOWN |
The list box of a combo box is dropping down. |
CBN_EDITCHANGE |
The user has changed text in the edit control. |
CBN_EDITUPDATE |
Altered text is about to be displayed. |
CBN_ERRSPACE |
The combo box is out of memory. |
CBN_KILLFOCUS |
The combo box is losing the input focus. |
CBN_SELCHANGE |
A new combo box list item is selected. |
Notification |
Meaning |
CBN_SELENDCANCEL |
The user's selection should be canceled. |
CBN_SELENDOK |
The user's selection is valid. |
CBN_SETFOCUS |
The combo box is receiving the input focus. |
Edit Notification |
|
EN_CHANGE |
The display is updated after text changes. |
EN_ERRSPACE |
The edit control is out of memory. |
EN_HSCROLL |
The user clicked the horizontal scrollbar. |
EN_KILLFOCUS |
The edit control is losing the input focus. |
EN_MAXTEXT |
The insertion is truncated. |
EN_SETFOCUS |
The edit control is receiving the input focus. |
EN_UPDATE |
The edit control is about to display altered text. |
EN_VSCROLL |
The user clicked the vertical scrollbar. |
List Box Notification |
|
LBN_DBLCLK |
The user double-clicked a string. |
LBN_ERRSPACE |
The list box is out of memory. |
LBN_KILLFOCUS |
The list box is losing the input focus. |
LBN_SELCANCEL |
The selection is canceled. |
LBN_SELCHANGE |
The selection is about to change. |
LBN_SETFOCUS |
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) protected procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER; procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE; end; ... procedure TSpecialPanel.CMMouseEnter(var Msg: TMessage); begin inherited; Color := clWhite; end; procedure TSpecialPanel.CMMouseLeave(var Msg: TMessage); begin inherited; Color := clBtnFace; end;
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):
const SX_MYMESSAGE = WM_USER + 100; begin SomeForm.Perform(SX_MYMESSAGE, 0, 0); { or } SendMessage(SomeForm.Handle, SX_MYMESSAGE, 0, 0); { or } PostMessage(SomeForm.Handle, SX_MYMESSAGE, 0, 0); . . . end;
Then create a normal message-handling procedure for this message in the form in which you want to handle the message:
TForm1 = class(TForm) . . . private procedure SXMyMessage(var Msg: TMessage); message SX_MYMESSAGE; end; procedure TForm1.SXMyMessage(var Msg: TMessage); begin MessageDlg('She turned me into a newt!', mtInformation, [mbOk], 0); end;
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.
CAUTION
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.
NOTE
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 controlsthanks 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:
var M: TMessage; begin with M do begin Message := UM_FOO; wParam := 0; lParam := 0; Result := 0; end; Panel1.Broadcast(M); end;