unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
const
MAX_ROW = 5; // ÃÖ´ëÇà¼ö
MAX_COL = 25; // ÃÖ´ëÄ÷³¼ö
var
Form1: TForm1;
implementation
{$R *.DFM}
// TMemoÀÇ WordWrap ÇÁ·ÎÆÛƼ´Â False ·Î ¼³Á¤
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
var
line, col: Integer;
begin
with Sender as TMemo do
begin
line := Perform(EM_LINEFROMCHAR, SelStart, 0);
col := SelStart - Perform(EM_LINEINDEX, line, 0);
if key = #8 then
begin
{ Do not allow backspace if caret is on first column and
deleting the linebreak of the line in front would result
in a line of more than MAX_COL characters. Damn inconvenient
for the user but specs are specs... }
if (col = 0) and (line > 0) then
begin
if (Length(lines[line])+Length(lines[line-1])) > MAX_COL then
Key := #0;
end; { If }
end { If }
else if key in [#13,#10] then
begin
{ Handle hard linebreaks via Enter or Ctrl-Enter }
if lines.count >= MAX_ROW then
begin
{ Max number of lines reached or exceeded, set caret
to start of next line or this line, if on the last. }
key := #0;
if line = (MAX_ROW-1) then
SelStart := Perform(EM_LINEINDEX, line,0)
else
SelStart := Perform(EM_LINEINDEX, line+1,0);
end; { If }
end { If }
else if Key >= ' ' then
begin
{ Do swallow key if current line has reached limit. }
if Length( lines[line] ) >= MAX_COL then
Key := #0;
end; { If }
end; { With }
if Key = #0 then
Beep;
end;
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
line, col: Integer;
begin
if Key = VK_DELETE then
with Sender as TMemo do
begin
line := Perform(EM_LINEFROMCHAR, SelStart, 0);
col := SelStart - Perform(EM_LINEINDEX, line, 0);
if col = Length(lines[line]) then
if (line < (MAX_ROW-1)) and ((Length(lines[line]) + Length(lines[line+1])) > MAX_COL) then
begin
key := 0;
Beep
end;
end;
end;
end. |
|