Name
Message Directive
Syntax
procedureName(var Msg); messageConstant;
Description
A method can be a message handler when
you use the message directive. A message handler
must take one var argument; usually this argument
is of type TMessage or one of the similar types in
the Messages unit. The only requirement Delphi
imposes is that the message argument be at least as big as a
Word. Delphi checks the first
Word of the message parameter to decide which
message handler to call.
The message handler must specify a constant message number. The message number is stored in the class’s RTTI.
Tips and Tricks
Message handlers and dynamic methods are stored in the same RTTI table. Delphi assigns small negative numbers to dynamic methods. To avoid conflicts with dynamic methods, do not use message handler numbers with the most significant bit set, that is, use numbers under $8000 (32,768).
Any class—not just controls and components—can define message handlers. Visual controls map Windows messages to Delphi messages, and the
Dispatchmethod ofTObjectcalls the appropriate message handler. In the past, messages were necessary to implement cross-type polymorphism, where objects could respond to a message regardless of base class. Interfaces have replaced this mechanism, so the only need for message handlers in new code is to handle Windows messages.Delphi defines many messages in its
Controlsunit. Anyone writing new controls or components should be aware of these messages and decide which ones a ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access