function GetCurLine(Memo: TCustomMemo): Longint;
begin
with Memo
do
Result := SendMessage(
Handle, EM_LINEFROMCHAR, SelStart, 0);
end;
function GetCurCol(Memo: TCustomMemo): Longint;
begin
with Memo
do
Result := SelStart - SendMessage(
Handle, EM_LINEINDEX, GetCurLine(Memo), 0);
end;
procedure TempCanvas(Font: TFont; Text:
string;
out TextWidth, TextHeight: Integer);
begin
with TBitmap.Create
do
try
Canvas.Font.Assign(Font);
TextWidth := Canvas.TextWidth(Text);
TextHeight := Canvas.TextHeight(Text);
finally
Free;
end;
end;
procedure TForm1.Memo1KeyPress(Sender: TObject;
var Key: Char);
const
MaxLen = 100;
MaxHeight = 100;
var
Memo: TMemo;
LineIndex,
LineCol,
TextWidth,
TextHeight: Integer;
s:
string;
begin
Memo := Sender
as TMemo;
LineIndex := GetCurLine(Memo);
LineCol := GetCurCol(Memo);
s := Memo.Lines[LineIndex];
TempCanvas(Memo.Font, s + Key, TextWidth, TextHeight);
if TextWidth > MaxLen
then
if Ord(Key) >= 32
then
Key := #0;
Memo.Lines[LineIndex] := s;
while (Memo.Lines.Count * TextHeight) > MaxHeight
do
Memo.Lines.Delete(Memo.Lines.Count - 1);
end;