Search code examples
winapiwindows-7delphi-7

Using "|" symbol for a hint-text in Delphi


For some reason when i specify "|" symbol inside Hint string for any Delphi control the hint terminates at the first occurred "|" so the tooltip window contains only part of the text till the first "|" encountered...

I've tested that symbol in C++ (both WinApi + MFC) and it shows hints pretty K with "|" symbol, so it looks like it's some Delphi specific bug.

The whole program works fine but this thing with hints just rly bugs me =\

So, any ideas how to fix that?


Solution

  • This is by design, and is documented behavior:

    TControl. Hint property

    TApplication.Hint property

    There are two parts to the Hint string--short and long--separated by the | character. Short hints are used by pop-up tool tips. Long hints are used by the status bar. Use the GetShortHint and GetLongHint global functions from the Controls unit to extract the long and short hints from a hint string.

    To "fix" this so you can display | characters, you have to either:

    Both are triggered while the pop-up is being prepared, after the Hint text is split and before the pop-up is actually displayed. Both provide access to a THintInfo record that you can customize as desired. It has a HintStr field you can set to whatever text you want, including | characters. And it has a HintControl field that points to the control that is displaying the pop-up.

    The simplest solution would be to use the OnShowHint event to set HintStr := HintControl.Hint.

    Using TApplication.OnShowHint:

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Application.OnShowHint := AppShowHint;
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      Application.OnShowHint := nil;
    end;
    
    procedure TForm1.AppShowHint(var HintStr: string; var CanShow: boolean; var HintInfo: THintInfo);
    begin
      HintStr := HintInfo.HintControl.Hint;
    end;
    

    Using TApplicationEvents.OnShowHint:

    procedure TForm1.ApplicationEvents1ShowHint(var HintStr: string; var CanShow: boolean; var HintInfo: THintInfo);
    begin
      HintStr := HintInfo.HintControl.Hint;
    end;