Usted puede utilizar el método TCanvas.TextRect, junto con las banderas tfCalcRect y tfWordBreak:
var
lRect : TRect;
lText : string;
begin
lRect.Left := 0;
lRect.Right := myWidth;
lRect.Top := 0;
lRect.Bottom := 0;
lText := myLabel.Caption;
myLabel.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
myLabel.Height := lRect.Bottom;
end;
TCanvas.TextRect
envuelve una llamada a la función DrawTextEx
de la API de Windows.
Las banderas tfCalcRect
y tfWordBreak
son envoltorios de Delphi para los valores DT_CALCRECT
y DT_WORDBREAK
de la API de Windows. Se puede encontrar información detallada acerca de sus efectos en la documentación DrawTextEx
en msdn
Debe agregar myLabel.Canvas.Font: = myLabel.Font antes de la línea TextRect. –