在Delphi / Lazarus中设置提示窗口(THintWindow)的大小

时间:2012-07-13 14:49:08

标签: delphi freepascal lazarus hint

我想在拉撒路做一个自定义提示。到目前为止,我已经在提示中动态加载了文本,并自定义了字体,字体大小和字体颜色。我想限制提示窗口的宽度。有任何想法吗?这是我的代码。

type
  TExHint = class(THintWindow)
  constructor Create(AOwner: TComponent); override;

...

constructor TExHint.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  with Canvas.Font do
  begin
    Name  := 'Hanuman';
    Size  := Size + 3;
  end;
  //Canvas.Width := ;
end;

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

我现在只有Lazarus来源和记事本,但我会尝试解释你如何使用THintWindow,因为这是最重要的理解:

  • 如果您将类名分配给HintWindowClass全局变量,那么您可以说注册您的提示窗口类,以供应用程序全局使用。然后每次应用程序将显示提示时,它将使用您的提示窗口类并调用您的覆盖函数以及您未覆盖的基础THintWindow类中的函数。以下是如何注册你的提示窗口类,以便在应用程序的范围内使用:

HintWindowClass := TExHint;
  • 为了获得提示窗口大小,只要显示提示,应用程序就会调用CalcHintRect函数。要自行调整提示窗口大小,您需要覆盖此功能,并且结果返回您想要的边界矩形。如果你不覆盖它,将使用基类(来自CalcHintRect类)的THintWindow函数,所以你应该覆盖它:

type
  TExHint = class(THintWindow)
  public
    constructor Create(AOwner: TComponent); override;
    function CalcHintRect(MaxWidth: Integer; const AHint: String;
      AData: Pointer): TRect; override;
  end;

constructor TExHint.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  with Canvas.Font do
  begin
    Name := 'Hanuman';
    Size := Size + 3;
  end;
end;

function TExHint.CalcHintRect(MaxWidth: Integer; const AHint: String;
  AData: Pointer): TRect;
begin
  // here you need to return bounds rectangle for the hint
  Result := Rect(0, 0, SomeWidth, SomeHeight);
end;

答案 1 :(得分:0)

您应该能够覆盖CreateParams并将宽度设置为您喜欢的任何宽度。

procedure TExHint.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.Width := X; 
end;

我没有对此进行过测试,但它应该有效。