### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

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

#### SirTwist

• New Member
• Posts: 18
##### 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.

Thanks in Advance and kind regards,
Sir Twist

#### RAW

• Hero Member
• Posts: 867
##### 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) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...