Recent

Author Topic: Height of Lines / Text  (Read 4574 times)

SirTwist

  • New Member
  • *
  • Posts: 21
Height of Lines / Text
« on: October 08, 2017, 06:58:42 pm »
Hi all,

I'm just making my first steps with RichMemo, and I would like to know the height in pixels of the (wordwrapped) text.

I want the Memo to shrink and grow vertically in respect of the text, especially when it is set via property "Rtf" the first time.

Any ideas about this?

Thanks in Advance and kind regards,
Sir Twist

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: Height of Lines / Text
« Reply #1 on: October 08, 2017, 08:31:36 pm »
I guess you can use DRAWTEXT (uses LCLIntf, LCLType) or write your own WordWrap-function. There is also WrapText, but it cannot give you any pixel information... I need this very soon too, if  I've got something useful I will post it here... It's interesting not only for a RichMemo but also in combination with a TLabel or TMemo...  :)

Play around with this...
Code: Pascal  [Select][+][-]
  1. Function WordWrapHeight(S: String; Ft: TFont; cMaxWidth: Cardinal): Integer;
  2.   Var
  3.    R  : TRect;
  4.    BMP: TBitmap;
  5.  Begin
  6.   R  := Rect(0, 0, cMaxWidth, 0);
  7.   BMP:= TBitmap.Create;
  8.    Try
  9.     BMP.Canvas.Font.Assign(Ft);
  10.     Result:= DrawText
  11.     (BMP.Canvas.Handle, PChar(S), Length(S), R, dt_CalcRect Or dt_WordBreak);
  12.    Finally
  13.     BMP.Free;
  14.    End;
  15.  End;
  16.  
  17.  
  18. Procedure TForm1.Button1Click(Sender: TObject);
  19.   Var
  20.    iWordWrap: Integer;
  21.  Begin
  22.   iWordWrap:= WordWrapHeight(Memo1.Text, Memo1.Font, Memo1.ClientWidth);
  23.   ShowMessage(IntToStr(iWordWrap)+' '+IntToStr(Memo1.ClientHeight));
  24.  End;
« Last Edit: October 08, 2017, 09:55:11 pm by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

 

TinyPortal © 2005-2018