在 TLabel 中显示最后一行

Show last line in a TLabel

我有一个固定高度和自动换行的 TLabel。问题是当标题文本超过标签的高度时,我看不到最后一行文本。我在整个互联网上搜索可以向下滚动并显示超过标题高度的最后一行文本的标签组件。

正如您在这张图片中看到的,第 7 行只显示了一半,而第 8 行甚至没有显示:

我希望第 1 行消失或上升,第 8 行完全可见。

您可以覆盖 TLabel 的 DoDrawText 虚拟方法。像这样的东西(使用插入器 class 的例子):

TLabel = class(StdCtrls.TLabel)
protected
  procedure DoDrawText(var Rect: TRect; Flags: Longint); override;
end;

... 

procedure TLabel.DoDrawText(var Rect: TRect; Flags: Longint);
var
  R: TRect;      
  TextHeight: Integer;
begin
  if (Flags and DT_CALCRECT = 0) then
  begin
    R := ClientRect;
    Canvas.Font := Font;
    DrawText(Canvas.Handle, PChar(Text), -1, R, DT_EXPANDTABS or DT_CALCRECT or DT_WORDBREAK);        
    TextHeight := R.Bottom - R.Top;    
    if TextHeight > ClientHeight then
      Rect.Top := Rect.Top - (TextHeight - ClientHeight);
  end;
  inherited DoDrawText(Rect, Flags);
end;

您可以使用 TScrollBox :

  • 丢一个TScrollBox.

  • TScrollBox.

  • 中拖放一个 TLabel
  • 将标签对齐设置为 alTop