How to determine how many lines a TMemo is capable of showing (Views: 100)


How to determine how many lines a TMemo is capable of showing


Here is the short and elegant way that works (most of the time):

function TForm1.MemoLinesShowing(memo: TMemo): integer;
  R: TRect;
  Memo.Perform(EM_GETRECT, 0, Longint(@R));
  Result := (R.Bottom - R.Top) div Canvas.TextHeight('XXX');

The problem with this code is that the TForm and the TMemo must both be using the same font. If the fonts are different, then the calculations are not accurate.

You have to retrieve the font height by selecting it into a device context. The reason you cannot use the font Height provided by Delphi is because Delphi caches the font infomation but doesn't acutally select the font into the DC (canvas) until it is actually going to draw something. This occurs in the painting event of the memo.

To get around this problem, you can get the memo's device context using the Windows API and get the font information from the device context to calculate the text height. The function below illustrates this process:

function TForm1.MemoLinesShowingLong(Memo: TMemo): integer;
  Oldfont: HFont; {the old font}
  DC: THandle; {Device context handle}
  i: integer; {loop variable}
  Tm: TTextMetric; {text metric structure}
  TheRect: TRect;
  DC := GetDC(Memo.Handle); {Get the memo's device context}
    {Select the memo's font}
    OldFont := SelectObject(DC, Memo.Font.Handle);
      GetTextMetrics(DC, Tm); {Get the text metric info}
      Memo.Perform(EM_GETRECT, 0, longint(@TheRect));
      Result := (TheRect.Bottom - TheRect.Top) div (Tm.tmHeight +
      SelectObject(DC, Oldfont); {Select the old font}
    ReleaseDC(Memo.Handle, DC); {Release the device context}

<< Back to main page