2009. február 26., csütörtök

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


Problem/Question/Abstract:

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

Answer:

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

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

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;
var
  Oldfont: HFont; {the old font}
  DC: THandle; {Device context handle}
  i: integer; {loop variable}
  Tm: TTextMetric; {text metric structure}
  TheRect: TRect;
begin
  DC := GetDC(Memo.Handle); {Get the memo's device context}
  try
    {Select the memo's font}
    OldFont := SelectObject(DC, Memo.Font.Handle);
    try
      GetTextMetrics(DC, Tm); {Get the text metric info}
      Memo.Perform(EM_GETRECT, 0, longint(@TheRect));
      Result := (TheRect.Bottom - TheRect.Top) div (Tm.tmHeight +
        Tm.tmExternalLeading);
    finally
      SelectObject(DC, Oldfont); {Select the old font}
    end;
  finally
    ReleaseDC(Memo.Handle, DC); {Release the device context}
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése