Author: Jonas Bilinkevicius
How would I find out how many viewed lines are in a memo? For example, if one line
is wrapped once, it would count as two. I need to stretch it so that all lines are
visible.
Answer:
Solve 1:
Adjusting a memo to the height required to show all text without scrollbars:
1 procedure TForm1.Button2Click(Sender: TObject);
2 var3 rect1, rect2: TRect;
4 S: string;
5 begin6 s := Memo1.Text;
7 memo1.Perform(EM_GETRECT, 0, longint(@rect1));
8 rect2 := rect1;
9 canvas.font := memo1.font;
10 DrawTextEx(canvas.handle, Pchar(S), Length(S), rect2, DT_CALCRECT or11 DT_EDITCONTROL or DT_WORDBREAK or DT_NOPREFIX, nil);
12 memo1.Height := memo1.height + rect2.bottom - rect1.bottom;
13 end;
Solve 2:
I use the following:
14 with TControlCanvas.Create do15 try16 Control := MmoView;
17 Font.Assign(MmoView.Font);
18 FFontHeight := TextHeight('Q');
19 FFontWidth :=
20 TextWidth('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') div 52;
21 finally22 Free;
23 end;
24 25 FMaxBuf := (MmoView.ClientHeight div FFontHeight) * (MmoView.ClientWidth div26 FFontWidth);
27 FMaxLines := (MmoView.ClientHeight div FFontHeight) - 1;