// A leading example: {create a Win32 project and add an onkeydown event to the form} procedure TBU. formkeydown (Sender: tobject; var key: word; shift: tshiftstate); Begin self. text: = char (key); end; {function: press a key on the keyboard. The title bar of the form displays the key name}
// Now we use the message method to re-implement this function unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs; Type tform1 = Class (tform) procedure keydown (var msg: twmkeydown); message wm_keydown; end; var form1: tform1; implementation {$ R *. DFM} procedure tform1.keydown (var msg: twmkeydown); Begin self. text: = char (MSG. charcode); end;
// Explain the definition of this message method: Procedure keydown (var msg: twmkeydown); message wm_keydown; {1. The maximum difference from other methods: one message is added; 2. The indicator is followed by the message name to be intercepted: wm_keydown; 3. It is a process, and the process name keydown is customized; 4. The parameter type is the parameter structure corresponding to the message, because twmkeydown is the rename of twmkey, you can also use twmkey; 5. The parameter name MSG is custom; 6. The parameter prefix must be var; 7. do not include any indicator when implementing the method .}
// If we put the above two functions together, when we press the next key? Which one will be executed? {Test} unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs; Type tform1 = Class (tform) {form onkeydown event definition} procedure formkeydown (Sender: tobject; var key: word; shift: tshiftstate); {wm_keydown message method definition} procedure keydown (var msg: twmkeydown ); message wm_keydown; end; var form1: tform1; implementation {$ R *. DFM} {implementation of form onkeydown event} procedure tform1.formkeydown (Sender: tobject; var key: word; shift: tshiftstate); begin showmessage ('event: '+ char (key )); end; {implementation of the wm_keydown message method} procedure tform1.keydown (var msg: twmkeydown); begin showmessage ('message: '+ char (MSG. charcode); end. {Test Result: only the message method is executed, and no event is executed, that is, the event is intercepted by the message}
// Can it coexist? Of course! {Change the message implementation to:} procedure tform1.keydown (var msg: twmkeydown); begin showmessage ('message: '+ char (MSG. charcode); inherited; end; {the message will be executed first, and then call the method} {change the message implementation to:} procedure tform1.keydown (var msg: twmkeydown); begin inherited; showmessage ('message: '+ char (MSG. charcode); end; {the method will be called first, and then the message will be executed}
{The concept of message is still very complicated. For example, wm_keydown is intercepted, and there are many messages in windows. For example, the concept of message in the current form is intercepted, there are many objects that can receive messages. There are also many message structures such as twmkey defined by Delphi, such as mouse messages... this is a topic that requires another column. in short, the message is powerful and can replace all events. We research it to solve what the event cannot .}