Adventures in Delphi 6 Messaging
IN THIS CHAPTER
- What Is a Message?
- Types of Messages
- How the Windows Message System Works
- Delphi's Message System
- Handling Messages
- Sending Your Own Messages
- Nonstandard Messages
- Anatomy of a Message System: VCL
- The Relationship Between Messages and Events
Although Visual Component Library (VCL) components expose many Win32 messages via Object Pascal events, it's still essential that you, the Win32 programmer, understand how the Windows message system works.
As a Delphi applications programmer, you'll find that the events surfaced by VCL will suit most of your needs; only occasionally will you have to delve into the world of Win32 message handling. As a Delphi component developer, however, you and messages will become very good friends because you have to directly handle many Windows messages and then invoke events corresponding to those messages.
The messaging capabilities covered in this chapter are specific to the VCL and aren't supported under the CLX environment. For more on the CLX architectures, see Chapters 10, "Component Architecture: VCL and CLX," and 13, "CLX Component Development."
What Is a Message?
A message is a notification of some occurrence sent by Windows to an application. Clicking a mouse button, resizing a window, or pressing a key on the keyboard, for example, causes Windows to send a message to an application notifying it of what occurred.
A message manifests itself as a record passed to an application by Windows. That record contains information such as what type of event occurred and additional information specific to the message. The message record for a mouse button click message, for example, contains the mouse coordinates at the time the button was pressed. The record type passed from Windows to the application is called a TMsg, which is defined in the Windows unit as shown in the following code:
type TMsg = packed record hwnd: HWND; // the handle of the Window for which the message // is intended message: UINT; // the message constant identifier wParam: WPARAM; // 32 bits of additional message-specific information lParam: LPARAM; // 32 bits of additional message-specific information time: DWORD; // the time that the message was created pt: TPoint; // Mouse cursor position when the message was created end;
What's in a Message?
Does the information in a message record look like Greek to you? If so, here's a little insight into what's what:
The 32-bit window handle of the window for which the message is intended. The window can be almost any type of screen object because Win32 maintains window handles for most visual objects (windows, dialog boxes, buttons, edits, and so on).
A constant value that represents some message. These constants can be defined by Windows in the Windows unit or by you through user-defined messages.
This field often contains a constant value associated with the message; it can also contain a window handle or the identification number of some window or control associated with the message.
This field often holds an index or pointer to some data in memory. Because wParam, lParam, and Pointer are all 32 bits in size, you can typecast interchangeably between them.
Now that you have an idea what makes up a message, it's time to take a look at some different types of Windows messages.