2

我正在尝试在 Lazarus 中进行自定义提示。到目前为止,我已经动态加载了提示中的文本,并自定义了字体、字体大小和字体颜色。我想限制提示窗口的宽度。有任何想法吗?这是我的代码。

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;

谢谢你的帮助。

4

2 回答 2

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;
于 2012-07-14T10:11:45.990 回答
0

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

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

我没有对此进行测试,但它应该可以工作。

于 2012-07-13T18:25:21.600 回答