您是华师大gislab的?
来自:delphi help
Message handlers are methods that implement responses to dynamically dispatched messages. Delphi抯 VCL uses message handlers to respond to Windows messages.
A message handler is created by including the message directive in a method declaration, followed by an integer constant between 1 and 49151 which specifies the message ID. For message handlers in VCL controls, the integer constant must be one of the Windows message IDs defined, along with corresponding record types, in the Messages unit. For example,
type
TTextBox = class(TCustomControl)
private
procedure WMChar(var Message: TWMChar); message WM_CHAR;
...
end;
A message handler must be a procedure that takes a single var parameter.
A message handler does not have to include the override directive to override an inherited message handler. In fact, it doesn抰 have to specify the same method name or parameter type as the method it overrides. The message ID alone determines which message the method responds to and whether it is an override.
Implementing message handlers
The implementation of a message handler can call the inherited message handler, as in this example:
procedure TTextBox.WMChar(var Message: TWMChar);
begin
if Chr(Message.CharCode) = #13 then
ProcessEnter
else
inherited;
end;
The inherited statement searches backward through the class hierarchy and invokes the first message handler with the same ID as the current method, automatically passing the message record to it. If no ancestor class implements a message handler for the given ID, inherited calls the DefaultHandler method originally defined in TObject.
The implementation of DefaultHandler in TObject simply returns without performing any actions. By overriding DefaultHandler, a class can implement its own default handling of messages. The DefaultHandler method for VCL controls calls the Windows DefWindowProc function.
Message dispatching
Message handlers are seldom called directly. Instead, messages are dispatched to an object using the Dispatch method inherited from TObject:
procedure Dispatch(var Message);
The Message parameter passed to Dispatch must be a record whose first entry is a field of type Cardinal containing a message ID. See the Messages unit for examples.
Dispatch searches backward through the class hierarchy (starting from the class of the object where it is called) and invokes the first message handler for the ID passed to it. If no message handler is found for the given ID, Dispatch calls DefaultHandler.