Mirror

How to adjust a memo to the height required to show all text without scrollbars (Views: 27)


Problem/Question/Abstract:

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:

procedure TForm1.Button2Click(Sender: TObject);
var
  rect1, rect2: TRect;
  S: string;
begin
  s := Memo1.Text;
  memo1.Perform(EM_GETRECT, 0, longint(@rect1));
  rect2 := rect1;
  canvas.font := memo1.font;
  DrawTextEx(canvas.handle, Pchar(S), Length(S), rect2, DT_CALCRECT or
    DT_EDITCONTROL or DT_WORDBREAK or DT_NOPREFIX, nil);
  memo1.Height := memo1.height + rect2.bottom - rect1.bottom;
end;


Solve 2:

I use the following:

with TControlCanvas.Create do
try
  Control := MmoView;
  Font.Assign(MmoView.Font);
  FFontHeight := TextHeight('Q');
  FFontWidth :=
    TextWidth('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') div 52;
finally
  Free;
end;

FMaxBuf := (MmoView.ClientHeight div FFontHeight) * (MmoView.ClientWidth div
  FFontWidth);
FMaxLines := (MmoView.ClientHeight div FFontHeight) - 1;

<< Back to main page