Mega Code Archive

 
Categories / Delphi / VCL
 

Getting the number of visible lines on a TMemo

Title: Getting the number of visible lines on a TMemo Question: How many lines are visible in my Memo component? Answer: Here's how to calculate the number of visible lines: function LinesVisible(Memo: TMemo): integer; Var OldFont : HFont; Hand : THandle; TM : TTextMetric; Rect : TRect; tempint : integer; begin Hand := GetDC(Memo.Handle); try OldFont := SelectObject(Hand, Memo.Font.Handle); try GetTextMetrics(Hand, TM); Memo.Perform(EM_GETRECT, 0, longint(@Rect)); tempint := (Rect.Bottom - Rect.Top) div (TM.tmHeight + TM.tmExternalLeading); finally SelectObject(Hand, OldFont); end; finally ReleaseDC(Memo.Handle, Hand); end; Result := tempint; end; Tested on W2K with delphi 4. Should be fine for other versions of windows and delphi. Last edited 28 apr 2000. Cheers to Paul Klerlein for pointing out a problem.