2006. november 14., kedd
How to set a TEdit or TMemo to overwrite instead of insert
Problem/Question/Abstract:
How to set a TEdit or TMemo to overwrite instead of insert
Answer:
Solve 1:
You have to fake it because the control does not natively support overtype mode. Provide overtype capability for edits and memos:
procedure TScratchMain.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
if (Sender is TCustomEdit) and Odd(GetKeyState(VK_INSERT)) then
with TCustomEdit(Sender) do
if SelLength = 0 then
case Key of
' '..#126, #128..#255:
begin
SelLength := 1;
if (SelLength > 0) and (SelText[1] = #13) then
SelLength := 2;
end;
end;
end;
With this handler the control will start out in insert mode since the state of VK_INSERT is not toggled by default. Pressing it once will toggle the key and put the control in overtype mode. If you want it to start out in overtype, use "not Odd(...)" in the If statement.
Solve 2:
I managed to simulate it by doing this (you need to declare the FOverwrite: boolean somewhere in the form):
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
type
TSmallPoint = packed record
case integer of
0: (x, y: Smallint);
1: (long: integer);
end;
var
CaretPos: TPoint;
sCaretPos: TSmallPoint;
begin
if (FOverwrite) and (Edit1.SelLength = 0) then
begin
GetCaretPos(CaretPos);
sCaretPos.x := CaretPos.x;
sCaretPos.y := CaretPos.y;
Edit1.SelStart := SendMessage(Edit1.Handle, EM_CHARFROMPOS, 0, sCaretPos.long);
Edit1.SelLength := 1;
Edit1.SelText := Key;
Key := #0;
end;
end;
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
case Key of
VK_INSERT: FOverwrite := not FOverwrite;
end;
end;
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése