Mirror

Get the correct height of a TDBText (Views: 707)


Problem/Question/Abstract:

I have a TDBText with WordWrap = True and it is anchored to the left and right of the form. As the form resizes, the height of the TDBText changes. Is there any way of knowing the height of the TDBText? TDBText.Height doesn't return the correct value.

Answer:

TDBText is a descendant of TCustomLabel, so this should work:

{ ... }
type
  TLabelCracker = class(TCustomLabel)
  end;

function LabelTextHeight(ALabel: TCustomLabel): Integer;
const
  WordWraps: array[Boolean] of Word = (0, DT_WORDBREAK);
var
  Rect: TRect;
begin
  Rect := ALabel.ClientRect;
  TLabelCracker(ALabel).DoDrawText(Rect, (DT_EXPANDTABS or DT_CALCRECT) or
    WordWraps[TLabelCracker(ALabel).WordWrap]);
  Result := Rect.Bottom - Rect.Top;
end;

<< Back to main page