Multi Line Popup Hints for Delphi VCL and FMX by Scott Hollows

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,438
Credits
573
Multi Line Popup Hints for Delphi VCL and FMX
Scott Hollows - 27/Oct/2016
[SHOWTOGROUPS=4,20]
Learn how to setup multi line popup hints in Delphi for any type of control for both VCL and FireMonkey.
This uses native Delphi code. You don’t need to hack the forms’s DFM text file

multi_line_hints_3




Take a short cut … download and use
Cant be bothered reading all this stuff ?
Just do this
  1. Download my utility units for VCL and FireMonkey.
    See the download link at the bottom of the page.
  2. Add a “~” character in your hint text.
    It will be replaced at runtime with a new line
  3. Then include this magic code in your form’s OnCreate event.
Код:
begin
  SetupMultiLineHintAll (self);
end;

One line of code .. .easy !

Read on if you want to geek out on the full details

The gory detail
This technique uses a flag character in the hint text (I like to use the tilde character ‘~’) that is replaced at runtime with an ASCII new line character (ASCII #10)

This is what it looks like in the IDE

multi_line_hints_1


This magic code will change the ~ character to a new line character
Код:
begin
  Button1.Hint := AnsiReplaceStr (
                         Button1.Hint,
                         '~',   // replace this
                         #10    // with this
                         );
end;

So now when you hover over the button the popup hint text will show in multiple lines like this

multi_line_hints_3


Generic Procedure
Instead of hard coding for one button, you can use a generic procedure that will work for any type of control.

These procedure will work in both VCL and FMX.
You just have to USE different units, as mentioned in the comments
Код:
procedure SetupMultiLineHint (aComponent : TComponent);
 // setup multi line hints for a single control
 // For VCL: uses VCL.controls, System.StrUtils
 // For FMX: uses FMX.controls, System.StrUtils
var
 s : string;
begin
 if aComponent is TControl then
    begin
    s := AnsiReplaceStr (TControl(aComponent).Hint ,'~',#10);
    if TControl(aComponent).Hint <> s then
       TControl(aComponent).Hint := s;
    end;
end;

UPDATE – Thanks To Mike for the bug report about TMenuItems not working (see comments below). Mike’s recommended changes have been implemented above

Then call it like this
Код:
begin
  SetupMultiLineHint (Button1);
  SetupMultiLineHint (Button2);
  SetupMultiLineHint (Edit1);
  SetupMultiLineHint (Label1);
end;

Apply To All Controls In A Form
The above example required you to specify each control.

You can can go one step further with a generic procedure that will setup multi-line hints for all controls on your form
Код:
procedure SetupMultiLineHintAll (
           aComponent : TComponent // form,frame,button,label,panel
           );
 // setup multi line hints for a component and all child components
 // convert all ~ characters in hints a new line/
 // For VCL uses VCL.controls,System.Classes
 //             ,VCL.Forms,System.StrUtils;
 // For FMX uses FMX.controls,System.Classes
 //             ,FMX.Forms,System.StrUtils;
var
 i : Integer;
begin
 if (aComponent is TControl) then // form and frame hint
    SetupMultiLineHint (aComponent as TControl);

     // Loop through all the components on the form
     // This finds components anywhere on the form
     // even within panels, tabs and frames
 for i := 0 to aComponent.ComponentCount -1 do
     if aComponent.Components [i] is TFrame then
        SetupMultiLineHintAll (aComponent.Components [i] as TFrame)
     else
        SetupMultiLineHint (aComponent.Components [i] as TControl);
end;

Then call it like this from the form’s OnCreate event
Код:
begin
  SetupMultiLineHintAll (self);
end;

Trouble Shooting
  • Popup hints do not display for TLabels in FireMonkey Delphi 10.1 Berlin)
    I think this is a bug in 10.1 FMX. It has nothing to do with multi-line hints as it occurs with just one line of text
  • #10 doesnt work
    #10 should work on all platforms, but I havent been able to test that due to my Mac Book being temporarily out of action.
    If it doesnt work, try the old trick of using all combinations of #10 and #13
    #10
    #13
    #10 + #13
    #13 + #10
Alternatives
You can also implement multi-line hints by modifying the text version of the form. Zarco Gajic

Download
Для просмотра ссылки Войди или Зарегистрируйся

This includes one unit for VCL and another for FMX.
Tested on Delphi 10.1 Berlin

To implement it, use this code in your forms OnCreate event
Код:
begin
  SetupMultiLineHintAll (self);
end;
[/SHOWTOGROUPS]