Search code examples
formsdelphipostmessage

Delphi - How trigger a Windows Message from a Form and retrieve it in a component that is in that Form using Delphi?


How trigger a Windows Message from a Form and retrieve it in a component that is in that Form using Delphi?

Ex:

In Form:

PostMessage(Handle, MY_MESSAGE, 0, 0);

In my Component (that is in same Form):

procedure OnMyMessage(var Msg: TMessage); message MY_MESSAGE;

in my case, OnMyMessage is simply not called!

The message is not only for this component, it is public, present on Form even if the component is not present. Thank you!


Solution

  • In the context of the form, Handle is the window handle of the form. If you wish for the component to be the recipient then you need to use the component's window handle, if it has one:

    PostMessage(MyComponent.Handle, MY_MESSAGE, 0, 0);
    

    Now, you also say:

    The message is not only for this component, it is public, present on Form even if the component is not present.

    If that means that you want the form to receive the message as well as the component you are out of luck. Messages have a single recipient. You may need to post to the form, handle the message in the form and then delegate to any other components that need to be notified.