首页  编辑  

计算Memo或者RichEdit每屏显示的行数

Tags: /超级猛料/VCL/Memo&Edit&Richedit/   Date Created:

这样:

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;

问题是,TForm 和TMemo 需要用同样的字体。

如果不同,可以用以下代码:

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;