TMemoの文字描画される位置
40 MemoDrawSize 動作確認 Delphi2007 更新日 2008/01/19(土)

TMemoの文字列が描画される縦方向の位置を調べてみました。

Panelを3つ、MemoとButtonを配置して実行してみました。
MemoのFontサイズはわかりやすいように大きくしただけです。
────────────────────
function GetTextHeight(Font: TFont;  s: String): Integer;
begin
  with TBitmap.Create do try
  Canvas.Font.Assign(Font);
  Result := Canvas.TextHeight(s);
  finally Free; end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  R: TRect;
  TrimHeight: Integer;
begin
  Memo1.Perform(EM_GETRECT, 0, Longint(@R));

  Panel1.BoundsRect := R;
  Panel1.Top := Memo1.BoundsRect.Top + 2;
  Panel1.Left := Memo1.BoundsRect.Left + 100;
  Panel1.Caption := Panel1.Caption + ':'
    + IntToStr(GetRectHeight(Panel1.BoundsRect));

  Panel2.BoundsRect := Panel1.BoundsRect;
  Panel2.Top := Memo1.BoundsRect.Top + 2;
  Panel2.Left := Memo1.BoundsRect.Left + 200;
  Panel2.Height := GetTextHeight(Memo1.Font, 'H');
  Panel2.Caption := Panel2.Caption + ':'
    + IntToStr(GetRectHeight(Panel2.BoundsRect));

  Panel3.BoundsRect := Panel1.BoundsRect;
  Panel3.Top := Memo1.BoundsRect.Top + 2;
  Panel3.Left := Memo1.BoundsRect.Left + 300;
  Panel3.Height := Abs(Memo1.Font.Height);
  Panel3.Caption := Panel3.Caption + ':' 
    + IntToStr(GetRectHeight(Panel3.BoundsRect));

end;
────────────────────

実行結果は次の通り



Panel1は
  Memo1.Perform(EM_GETRECT, 0, Longint(@R));
で求まる、テキストが描画される領域のRect

Panel2は
  Panel2.Height := GetTextHeight(Memo1.Font, 'H');
で求まる、Fontの高さの最大値

Panel3は
  Panel3.Height := Abs(Memo1.Font.Height);
で求まる、Font.Heightが返す値の高さ

になります。Font.Heightは描画されている部分の
上から下端までの値を示す事がわかります。

参考────────────────────
MiyaB ソフト Delphi Tips
http://www.miyab.com/delphi_tips.html#tmemo