Intercepting hints on delphi

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

I tried to catch CM_SHOWHINT , but this event seems to be only of the form, but not of the component itself.

CustomHint insertion doesn’t really work because it accepts text from the Hint property.

my component is a descendant of TCustomPanel

Here is what I am trying to do:

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

I found this code somewhere on the Internet, unfortunately, it does not work.

+4
source share
2 answers

CM_HINTSHOW really what you need. Here is 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 cannot call inherited , which is probably the reason for its failure. Or perhaps the class declaration omits the override on WndProc . It doesn't matter, it's cleaner as I have in this answer.

+8
source

You can use the OnShowHint event

It has a HintInfo parameter: http://docwiki.embarcadero.com/Libraries/XE3/en/Vcl.Forms.THintInfo

This parameter allows you to request a tooltip control, tooltip text and all this context - and, if necessary, override them.

If you want to filter which components change the tooltip, you can, for example, declare some kind of IT temporaryHint interface, for example

 type ITemporaryHint = interface ['{1234xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}'] function NeedCustomHint: Boolean; function HintText: string; end; 

You can then check out later on any of your components to see if they implement this interface.

 procedure TForm1.DoShowHint(var HintStr: string; var CanShow: Boolean; var HintInfo: THintInfo); var ih: ITemporaryHint; begin if Supports(HintInfo.HintControl, {GUID for ITemporaryHint}, ih) then if ih.NeedCustomHint then HintInfo.HintStr := ih.HintText; end; procedure TForm1.FormCreate(Sender: TObject); begin Application.ShowHint := True; Application.OnShowHint := DoShowHint; end; 
+6
source

Source: https://habr.com/ru/post/1439132/


All Articles