Home > Articles > Programming > General Programming/Other Languages

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

This chapter is from the book

Delphi's Message System

VCL handles many of the details of the Windows message system for you. The message loop is built into VCL's Forms unit, for example, so you don't have to worry about fetching messages from the queue or dispatching them to a window procedure. Delphi also places the information located in the Windows TMsg record into a generic TMessage record:

type
 TMessage = record
 Msg: Cardinal;
 case Integer of
  0: (
  WParam: Longint;
  LParam: Longint;
  Result: Longint);
  1: (
  WParamLo: Word;
  WParamHi: Word;
  LParamLo: Word;
  LParamHi: Word;
  ResultLo: Word;
  ResultHi: Word); 
 end;

Notice that TMessage record has a little less information than does TMsg. That's because Delphi internalizes the other TMsg fields; TMessage contains just the essential information you need to handle a message.

It's important to note that the TMessage record also contains a Result field. As mentioned earlier, some messages require the window procedure to return some value after processing a message. With Delphi, you accomplish this process in a straightforward fashion by placing the return value in the Result field of TMessage. This process is explained in detail later in the section "Assigning Message Result Values."

Message-Specific Records

In addition to the generic TMessage record, Delphi defines a message-specific record for every Windows message. The purpose of these message-specific records is to give you all the information the message offers without having to decipher the wParam and lParam fields of a record. All the message-specific records can be found in the Messages unit. As an example, here's the message record used to hold most mouse messages:

type
 TWMMouse = packed record
 Msg: Cardinal;
 Keys: Longint;
 case Integer of
  0: (
  XPos: Smallint;
  YPos: Smallint);
  1: (
  Pos: TSmallPoint;
  Result: Longint);
 end; 

All the record types for specific mouse messages (WM_LBUTTONDOWN and WM_RBUTTONUP, for example) are simply defined as equal to TWMMouse, as in the following example:

TWMRButtonUp = TWMMouse;
TWMLButtonDown = TWMMouse;

NOTE

A message record is defined for nearly every standard Windows message. The naming convention dictates that the name of the record must be the same as the name of the message with a T prepended, using camel capitalization and without the underscore. For example, the name of the message record type for a WM_SETFONT message is TWMSetFont.

By the way, TMessage works with all messages in all situations but isn't as convenient as message-specific records.

  • + Share This
  • 🔖 Save To Your Account