【问题标题】:Setting size of hint window (THintWindow) in Delphi/Lazarus在 Delphi/Lazarus 中设置提示窗口 (THintWindow) 的大小
【发布时间】:2012-07-13 10:41:05
【问题描述】:

我正在尝试在 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;

感谢您的帮助。

【问题讨论】:

  • 覆盖CalcHintRect 函数。该函数返回的矩形将是提示大小。
  • 对不起,新手跟进。您能否提供代码,以便我知道如何以及在何处实现?

标签: delphi freepascal lazarus hint


【解决方案1】:

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

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

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

【讨论】:

  • 我在哪里放置和调用这个过程?
【解决方案2】:

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

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

HintWindowClass := TExHint;
  • 为了获得提示窗口大小,应用程序在提示显示时调用CalcHintRect 函数。要自行调整提示窗口大小,您需要覆盖此函数并
    作为结果返回您想要的边界矩形。如果您不覆盖它,则将使用基类(来自 THintWindow 类)中的 CalcHintRect 函数,因此您应该覆盖它:

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;

【讨论】:

  • @preah - 试试 'Result := classes.Rect(..'
  • @Preah - 先调用继承的方法:Result:=inherited CalcHintRect(MaxWidth,Hint,AData);,然后随意更改Result.Left、Result.Right。
  • @TLama - 也许调用继承的方法只指定 MaxWidth 更好?
  • 设置 Result.rightResult.bottom 实际上与 TLama 的解决方案做同样的事情。我希望自动设置高度,但我设置了宽度。
  • 如果Hint 参数与'' 不同,则否。在这种情况下,计算高度以适合Hint 文本。如果您通过'',那么您将获得(几乎)屏幕的高度。对你来说(如果你说我设置的宽度)将足以自己计算矩形而无需inherited 调用。
猜你喜欢
  • 2010-11-07
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-02
  • 1970-01-01
  • 2022-07-05
  • 2021-11-09
相关资源
最近更新 更多