在memo中的实现 procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char); var col,lin:integer;old,new:string; begin col:=form1.Memo1.CaretPos.X; lin:=form1.Memo1.CaretPos.Y; old:=memo1.Lines[lin]; new:=copy(old,1,col)+key+copy(old,(col+2),(length(old)-col-1)); memo1.Lines[lin]:=new; key:=#0; form1.Memo1.SelStart:=col; end;
应该是这样的 han:boolean; 全局变量var col,lin:integer;old,new:string;i,j:integer; begin if ((key in ['0'..'9']) or (ord(key)>128) or (key in ['a'..'z']) or (key in ['A'..'Z'])) then begin if ord(key)>128 then begin if not han then exit; end; han:=false; col:=form1.Memo1.CaretPos.X; lin:=form1.Memo1.CaretPos.Y; old:=memo1.Lines[lin]; if old='' then exit; if ord(old[col+1])>128 then new:=copy(old,1,col)+key+copy(old,(col+3),(length(old)-col-1)) else new:=copy(old,1,col)+key+copy(old,(col+2),(length(old)-col-1)); memo1.Lines[lin]:=new; key:=#0; j:=0; for i:=1 to lin do begin j:=j+length(memo1.Lines[i-1]); end; form1.Memo1.SelStart:=j+col+1; end; end;
老大,如此复杂?用Api不就得了?GetKbStatus;
你在你的keypress里判断一下全局变量就行了。 if flag=inserted then begin …… end else ……
to crossbow(La Vida Es Amor) GetKbStatus 是干什么用的?我怎么找不到
car flag_insert:boolean;// true 为插入,false为覆盖
在Form的OnKeyDown事件中写
if key=VK_Insert then
flag_insert:= not flag_insert;
应该不是很难吧
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
var col,lin:integer;old,new:string;
begin
col:=form1.Memo1.CaretPos.X;
lin:=form1.Memo1.CaretPos.Y;
old:=memo1.Lines[lin];
new:=copy(old,1,col)+key+copy(old,(col+2),(length(old)-col-1));
memo1.Lines[lin]:=new;
key:=#0;
form1.Memo1.SelStart:=col;
end;
begin
if ((key in ['0'..'9']) or (ord(key)>128) or (key in ['a'..'z']) or (key in ['A'..'Z'])) then
begin
if ord(key)>128 then
begin
if not han then
exit;
end;
han:=false;
col:=form1.Memo1.CaretPos.X;
lin:=form1.Memo1.CaretPos.Y;
old:=memo1.Lines[lin];
if old='' then
exit;
if ord(old[col+1])>128 then
new:=copy(old,1,col)+key+copy(old,(col+3),(length(old)-col-1))
else
new:=copy(old,1,col)+key+copy(old,(col+2),(length(old)-col-1));
memo1.Lines[lin]:=new;
key:=#0;
j:=0;
for i:=1 to lin do
begin
j:=j+length(memo1.Lines[i-1]);
end;
form1.Memo1.SelStart:=j+col+1;
end;
end;
if flag=inserted then
begin
……
end
else
……