Search code examples
delphidelphi-xe2windows-messageshint

Intercepting hint event on delphi


I am trying to change hint text temporarily at a run-time inside of a component, without changing the Hint property itself.

I've tried catching CM_SHOWHINT, but this event seems to only come to form, but not the component itself.

Inserting CustomHint doesnt really work either, because it takes the text from the Hint property.

my component is descendant from TCustomPanel

Here's what i'm trying to do:

procedure TImageBtn.WndProc(var Message: TMessage);
begin
  if (Message.Msg = CM_HINTSHOW) then
    PHintInfo(Message.LParam)^.HintStr := 'CustomHint';
end;

I've found this code somewhere in the internet, unfortunately it doesn't work tho.


Solution

  • CM_HINTSHOW is indeed just what you need. Here's a simple example:

    type
      TButton = class(Vcl.StdCtrls.TButton)
      protected
        procedure CMHintShow(var Message: TCMHintShow); message CM_HINTSHOW;
      end;
    
      TMyForm = class(TForm)
        Button1: TButton;
      end;
    
    ....
    
    procedure TButton.CMHintShow(var Message: TCMHintShow);
    begin
      inherited;
      if Message.HintInfo.HintControl=Self then
        Message.HintInfo.HintStr := 'my custom hint';
    end;
    

    The code in the question is failing to call inherited which is probably why it fails. Or perhaps the the class declaration omits the override directive on WndProc. No matter, it's cleaner the way I have it in this answer.