动态创建的THTMLabel.Height总是返回默认值?

发布于 2025-01-08 12:29:57 字数 809 浏览 0 评论 0原文

我正在创建许多动态创建的 THTMLabels,但是在创建这些标签之后,当我尝试获取它的高度时,它总是返回默认高度值。

这是我的代码:

for i := 0 to ASentencePtr^.MUS.Count - 1 do
begin
  j := Random(slTemp.Count);
  sSen := ASentencePtr^.MUS.Strings[StrToInt(slTemp.Strings[j])] + ' / ';

  THTMLabel.Create(Self).Name := 'lblSen_' + slTemp.Strings[j];
  with THTMLabel(FindComponent('lblSen_' + slTemp.Strings[j])) do
  begin
    Font.Size := 18;
    Font.Style := [fsBold];
    Parent := FlowPanel1;
    Width := Parent.Width;
    Cursor := crHandPoint;
    DragMode := dmAutomatic;
    ControlStyle := ControlStyle + [csDisplayDragImage];
    HTMLText.Add(sSen);
    Autosizing := True;        
  end;

  slTemp.Delete(j);
end;

现在,当我尝试访问 THTMLabel(FindComponent('lblSen_0')).Height 时,它仅返回默认值 17。我哪里出错了?有人有什么想法吗?非常感谢任何帮助,谢谢。

I am creating a number of dynamically created THTMLabels but after these are created,when I try to get it's height,it always return the default height value.

Here is my code:

for i := 0 to ASentencePtr^.MUS.Count - 1 do
begin
  j := Random(slTemp.Count);
  sSen := ASentencePtr^.MUS.Strings[StrToInt(slTemp.Strings[j])] + ' / ';

  THTMLabel.Create(Self).Name := 'lblSen_' + slTemp.Strings[j];
  with THTMLabel(FindComponent('lblSen_' + slTemp.Strings[j])) do
  begin
    Font.Size := 18;
    Font.Style := [fsBold];
    Parent := FlowPanel1;
    Width := Parent.Width;
    Cursor := crHandPoint;
    DragMode := dmAutomatic;
    ControlStyle := ControlStyle + [csDisplayDragImage];
    HTMLText.Add(sSen);
    Autosizing := True;        
  end;

  slTemp.Delete(j);
end;

Now when I try to access THTMLabel(FindComponent('lblSen_0')).Height, it returns only the default value which is 17. Where have I gone wrong? Any thoughts anyone? Any help is greatly appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

司马昭之心 2025-01-15 12:29:58

我们遇到了同样的问题,但设法使用 THTMLStaticText 组件和这个在动态(高度)调整时计算高度的函数解决了这些问题:

function CalculateDynamicHeight( aLabel: TLabel; htmlStaticText: THTMLStaticText): Integer;
var
  lRect : TRect;
  lText : string;
begin
  lRect := Rect( 0, 0, htmlStaticText.Width, 0);
  lText := htmlStaticText.Text;

  aLabel.Caption := htmlStaticText.Text;
  aLabel.Font := htmlStaticText.Font;
  aLabel.Canvas.Font := htmlStaticText.Font;
  aLabel.Canvas.TextRect(
            {var} lRect, //will be modified to fit the text dimensions
            {var} lText, //not modified, unless you use the "tfModifyingString" flag
            [tfCalcRect, tfWordBreak] //flags to say "compute text dimensions with line breaks"
          );
  ASSERT( lRect.Top = 0 ); //this shouldn't have moved
  aLabel.Height := lRect.Bottom;

  Result := lRect.Bottom;
end;

该函数需要一个 TLabel 组件,专门用于计算目的(您可以将可见性设置为 false)。 htmlStaticText 组件应将 AutoSize 设置为 true(在我们的示例中,AutoSizeType 设置为 asVertical),并且在调用该函数时应存在 htmlStaticText.Text。

We had the same problems but managed to solve them with the THTMLStaticText component and this function that calculates the height when dynamically (height) adjusted:

function CalculateDynamicHeight( aLabel: TLabel; htmlStaticText: THTMLStaticText): Integer;
var
  lRect : TRect;
  lText : string;
begin
  lRect := Rect( 0, 0, htmlStaticText.Width, 0);
  lText := htmlStaticText.Text;

  aLabel.Caption := htmlStaticText.Text;
  aLabel.Font := htmlStaticText.Font;
  aLabel.Canvas.Font := htmlStaticText.Font;
  aLabel.Canvas.TextRect(
            {var} lRect, //will be modified to fit the text dimensions
            {var} lText, //not modified, unless you use the "tfModifyingString" flag
            [tfCalcRect, tfWordBreak] //flags to say "compute text dimensions with line breaks"
          );
  ASSERT( lRect.Top = 0 ); //this shouldn't have moved
  aLabel.Height := lRect.Bottom;

  Result := lRect.Bottom;
end;

The function requires a TLabel component, used exclusively for calculation purposes (you can set the visibility to false). The htmlStaticText component should have AutoSize set to true (in our case AutoSizeType is set to asVertical) and the htmlStaticText.Text should be present when calling the function.

彼岸花ソ最美的依靠 2025-01-15 12:29:58

我认为 THTMLLabel 是 Jedi Library 的一部分,不是吗?这个问题很奇怪。我不知道该控件的实现,但如果您遇到此问题,则可能是 AutoSize 属性的实现非常糟糕。

如果您无法检查并修复此控件的源,请尝试使用 BoundsRect 属性来获取高度:

LabelHeight := THTMLabel(FindComponent('lblSen_0')).BoundsRect.Bottom;

I think THTMLLabel is part of the Jedi Library, itsn't? This problem is weird. I don't know the implementation of this control, but if you are having this problem, then there's may be a very bad implementation of the AutoSize Property.

If you can't inspect and fix the source of this control, try to use the BoundsRect property to get the height:

LabelHeight := THTMLabel(FindComponent('lblSen_0')).BoundsRect.Bottom;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文