delphitooltiphint

Delphi HintWindow with padding


I'd like to create some padding to all hints shown in my Delphi 12 application. I am already setting the global HintWindowClass to a custom class derived from THintWindow, and I thought its constructor may create that padding. But everything I did so far has no effect on the hint window:

HintWindowClass := TMyHintWindow;

...

constructor TMyHintWindow.Create(AOwner: TComponent);
begin
  inherited;
  AlignWithMargins := True;
  Margins.Left := 10;
  Margins.Right := 10;
  Padding.Left := 10;
  Padding.Right := 10;
end;

Do I have to manipulate the canvas in some way?


Solution

  • The hint window does not use controls inside to display the hint, but rather paints it on the canvas. Therefore anything tweaked with alignments won't work. Better override CalcHintRect and Paint.

    To avoid copying most of the inherited code of THintWindow you can use this approach:

      TMyHintWindow = class(THintWindow)
      private
        FPainting: Boolean;
      protected
        function GetClientRect: TRect; override;
        procedure Paint; override;
      public
        function CalcHintRect(MaxWidth: Integer; const AHint: string; AData: TCustomData): TRect; override;
      end;
    
    function TMyHintWindow.CalcHintRect(MaxWidth: Integer; const AHint: string; AData: TCustomData): TRect;
    begin
      Result := inherited;
      Result.Width := Result.Width + 2*ScaleValue(10);
      Result.Height := Result.Height + 2*ScaleValue(10);
    end;
    
    function TMyHintWindow.GetClientRect: TRect;
    begin
      Result := inherited;
      if FPainting then
        Result.Inflate(-ScaleValue(10), -ScaleValue(10));
    end;
    
    procedure TMyHintWindow.Paint;
    begin
      FPainting := True;
      try
        inherited;
      finally
        FPainting := False;
      end;
    end;