Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Message handlers are methods, and they therefore must be declared and implemented within the same unit as the class they belong to.</p> <p>That doesn't stop you from delegating the real work to some other function in another unit, though. There's nothing special about message handlers in that regard. Declare whatever function you like, and pass it whatever parameters it will need in order to accomplish its duties. If all it needs are the contents of the <code>TMessage</code> record, then so be it. However, it will probably also need to know what form received the message, so pass a reference to that, too. Or maybe it just needs to know the values of some of the form's private fields, so pass those instead.</p> <p>You could make a unit for handling messages. Its interface section could look like this:</p> <pre><code>unit MessageHandlers; interface uses OtherUnit; procedure HandleWMInput(Form: FOtherUnit; var Message: TWMInput); </code></pre> <p>There's nothing to designate that function as handling <code>wm_Input</code> messages; the <code>message</code> directive is only for classes.</p> <p>Then, the implementation section of your form's unit can <em>use</em> that unit and call its functions:</p> <pre><code>uses MessageHandlers; procedure FOtherForm.WMInput(var Message: TWMInput); begin HandleWMInput(Self, Message); end; </code></pre> <hr> <p>I've used <code>TWMInput</code> instead of <code>TMessage</code>. If Delphi doesn't already declare that type for you (in <em>Messages.pas</em>), then I suggest you declare it yourself. It lets Delphi perform the message-parameter cracking for you. For example, with the declaration below, the raw-input handle has a better type and name than the corresponding field in <code>TMessage</code>.</p> <pre><code>type TWMInput = packed record Msg: Cardinal; InputCode: Byte; Unused: array[0..2] of Byte; RawInput: HRawInput; Result: LongInt; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload