最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

tooltip - Delphi HintWindow with padding - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Feb 16 at 21:42 AmigoJack 6,1082 gold badges19 silver badges34 bronze badges asked Feb 16 at 16:41 AnseAnse 1,66014 silver badges28 bronze badges 2
  • 2 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. – Uwe Raabe Commented Feb 16 at 17:00
  • Thank you, that works indeed! One needs to increase the surrounding rect in .CalcHintRect and move the text rect in .Paint. – Anse Commented Feb 16 at 18:02
Add a comment  | 

1 Answer 1

Reset to default 2

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;
发布评论

评论列表(0)

  1. 暂无评论