3. Create a new Message processing method
Because Delphi only provides a way to handle most ordinary Windows messages, when you define your own message, you create a new message-handling method.
The process of user-defined messages includes two aspects:
To define your own message
Declaring a new message processing method
⑴ define your message.
Many standard parts define messages for internal use. The most common driver for defining messages is broadcast information and notification of state changes.
The process of defining a message is two-step:
Declaring a message identifier
Declaring message record types
① Declaration Message Identity
The message ID is a constant of integer size. Windows saves less than 1024 messages for its own use, so when declaring your message, you should be greater than 1024.
The constant Wm_user represents the start number used for custom messages. When defining a message standard, you should be based on Wm_user.
Some standard Windows controls messages that use user-defined scopes, including ListBox, ComboBox, EditBox, and button. If you inherit a part from the above part, when you define a new message, you should check to see if the message unit has messages for that control.
The method for defining the message is as follows:
Const
wm_myfirstmessage=wm_user+0;
wm_mysecondmessage=wm_user+1;
② declaring message record types
If you want to give a meaningful name to the parameter of a custom message, declare the message record type for the message. Message logging is the type of parameter passed to the message processing method. If you do not use message parameters or want to use old style parameters, you can use the default message record.
To declare a message record type, follow these rules
Name the message record type as a message name, preceded by T
Name the first field in the record as MSG, type Tmsgpraram
Define the next two bytes as word in response to Word size parameters
Match the next four bytes to the long argument
Name the last field as result, and the type is Longint
Here is the definition of twmmouse
Type
Twmmouse=record
Msg:tmsgparam; {The first one is message ID}
Keys:word; {WParam}
Case Integer of {Two ways to define LPARAM}
O: (
Xpos:integer; {or with x,y coordinates}
Ypos:integer);
1: (
Pos:tpoint; {or as a single point}
Result:longint; ) {Finally the result field}
End
Twmmouse uses variable-length records to define different sets of names for the same parameters.
⑵ declares a new message processing method
There are two types of environments that require you to define new message processing methods:
Customizing new parts requires handling Windows messages that are not processed by standard parts
New message defined for custom part use
The way to declare a message processing method is as follows:
Protected part declaration method in a part declaration
To make a method into a process
Named method with the message name to be processed but not underlined
Pass a var parameter named message, type is a messaging record type
Write a special handling code for this part
Calling an inherited message method
The following is the message handling code for user-defined message cm_changecolor:
Type
Tmycomponent=class (Tcontrol)
...
Protected
Procedure Cmchangecolor (var message:tmessage);
Message Cm_changecolor;
End
Procedure Tmycomponent.cmchangecolor (var message:tmessage);
Begin
Color: = Message LParam;
inherited;
End