📄 ieditcustom.pas
字号:
if SelLength <> 0 then ClearSelection
else
begin
Delete(FText, CursorPos, 1);
CursorPos := CursorPos - 1;
end;
end;
iVK_RETURN : begin
CompleteChange;
SelectAll;
end;
iVK_ESCAPE : begin
Undo;
end;
iVK_CANCEL : begin //Ctrl-C
CopyToClipBoard;
end;
iVK_CTRL_V : begin //Ctrl-V
PasteFromClipBoard;
end;
iVK_CTRL_X : begin //Ctrl-X
CutToClipBoard;
end;
iVK_CTRL_Z : begin //Ctrl-Z
Undo;
end;
iVK_CTRL_A : begin //Ctrl-A
SelectAll;
end;
else begin
if AllowKey(Key) then
begin
if FMaxLength <> 0 then
begin
if (Length(FText) < MaxLength) or (FSelLength <> 0) then InsertText(Key) else Beep;
end
else InsertText(Key);
end;
end
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.iKeyDown(var CharCode: Word; Shift: TShiftState);
begin
if ssShift in Shift then if SelLength = 0 then
begin
FMouseDownCharPos := CursorPos;
end;
case CharCode of
iVK_LEFT : begin
if ssCtrl in Shift then CursorHome(Shift) else CursorLeft(Shift);
end;
iVK_RIGHT : begin
if ssCtrl in Shift then CursorEnd(Shift) else CursorRight(Shift);
end;
iVK_HOME : begin
CursorHome(Shift);
end;
iVK_END : begin
CursorEnd(Shift);
end;
iVK_DELETE : begin
if SelLength <> 0 then ClearSelection
else Delete(FText, CursorPos + 1, 1);
InvalidateChange;
end;
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.CursorHome(Shift: TShiftState);
begin
CursorPos := 0;
if ssShift in Shift then SelectCalc(FMouseDownCharPos, CursorPos)
else SelLength := 0;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.CursorEnd(Shift: TShiftState);
begin
CursorPos := Length(FText);
if ssShift in Shift then SelectCalc(FMouseDownCharPos, CursorPos)
else SelLength := 0;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.CursorLeft(Shift: TShiftState);
begin
CursorPos := CursorPos - 1;
if ssShift in Shift then SelectCalc(FMouseDownCharPos, CursorPos)
else SelLength := 0;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.CursorRight(Shift: TShiftState);
begin
CursorPos := CursorPos + 1;
if ssShift in Shift then SelectCalc(FMouseDownCharPos, CursorPos)
else SelLength := 0;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.InsertText(Value: String);
begin
ClearSelection;
Insert(Value, FText, CursorPos+1);
CursorPos := CursorPos + Length(Value);
InvalidateChange;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.CopyToClipBoard;
begin
if ErrorActive then Clipboard.AsText := GetDisplayText(ErrorText)
else if SelText <> '' then Clipboard.AsText := GetDisplayText(SelText);
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.CutToClipBoard;
begin
if ErrorActive then Clipboard.AsText := ErrorText
else
begin
if SelText <> '' then Clipboard.AsText := GetDisplayText(SelText);
ClearSelection;
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.PasteFromClipBoard;
var
AText : String;
begin
if ErrorActive then Exit;
if FMaxLength = 0 then
begin
InsertText(Clipboard.AsText);
end
else
begin
AText := Clipboard.AsText;
AText := Copy(AText, 1, MaxLength - Length(FText));
InsertText(AText);
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.SelectAll;
begin
SelStart := 0;
SelLength := Length(FText);
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.ClearSelection;
begin
if SelLength = 0 then Exit;
Delete(FText, FSelStart + 1, FSelLength);
CursorPos := FSelStart;
SelLength := 0;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.iDoubleClick;
begin
FDoubleClickActive := True;
SelectAll;
end;
//****************************************************************************************************************************************************
function TiEditCustom.GetSelText: String;
begin
Result := Copy(FText, FSelStart + 1, FSelLength);
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.PopupMenuOpen(Sender: TObject);
begin
FApplyMenuItem.Enabled := (FUndoText <> FText) and (not ErrorActive);
FUndoMenuItem.Enabled := (FUndoText <> FText) and (not ErrorActive);
FCutMenuItem.Enabled := (SelText <> '') and (not ErrorActive);
FCopyMenuItem.Enabled := (SelText <> '') or ( ErrorActive);
FPasteMenuItem.Enabled := (Clipboard.AsText <> '') and (not ErrorActive);
FDeleteMenuItem.Enabled := (SelText <> '') and (not ErrorActive);
FSelectAllMenuItem.Enabled := FSelLength <> Length(FText);
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.ApplyMenuItemClick(Sender: TObject);
begin
UserGenerated := True;
try
Apply
finally
UserGenerated := False;
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.PasteMenuItemClick (Sender: TObject);begin PasteFromClipBoard;end;
procedure TiEditCustom.UndoMenuItemClick (Sender: TObject);begin Undo end;
procedure TiEditCustom.CutMenuItemClick (Sender: TObject);begin CutToClipBoard; end;
procedure TiEditCustom.CopyMenuItemClick (Sender: TObject);begin CopyToClipBoard; end;
procedure TiEditCustom.DeleteMenuItemClick (Sender: TObject);begin ClearSelection; end;
procedure TiEditCustom.SelectAllMenuItemClick(Sender: TObject);begin SelectAll; end;
//****************************************************************************************************************************************************
procedure TiEditCustom.Apply;
begin
CompleteChange;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.Undo;
begin
FText := FUndoText;
SelLength := 0;
CursorHome([]);
if FAutoSelect then SelectAll;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.CompleteChange;
begin
if FText <> FUndoText then
begin
DoChange;
FUndoText := FText;
if FAutoSelect then SelectAll;
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.iSetAutoSize(const Value: Boolean);
begin
if FAutoSize <> Value then
begin
FAutoSize := Value;
DoAutoSize;
InvalidateChange;
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.DoAutoSize;
begin
if FDoingAutoSize then Exit;
if csLoading in ComponentState then Exit;
if not Assigned(Parent) {$ifdef iVCL}and (ParentWindow = 0){$endif} then Exit;
if FAutoSize then
begin
FDoingAutoSize := True;
try
with Canvas do
begin
Font.Assign(FFont);
Height := 2*GetBorderMargin + 4 + TextHeight('ABC');
end;
if Assigned(FOnAutoSize) then FOnAutoSize(Self);
finally
FDoingAutoSize := False;
end;
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.Clear;
begin
Text := '';
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.ClearUndo;
begin
FUndoText := Text;
end;
//****************************************************************************************************************************************************
function TiEditCustom.GetCanUndo: Boolean;
begin
Result := FUndoText <> FText;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.SetSelText(const Value: String);
var
SubStringPos : Integer;
begin
SubStringPos := POS(Value, FText);
if SubStringPos <> 0 then
begin
FSelStart := SubStringPos - 1;
FSelLength := Length(Value);
end;
end;
//****************************************************************************************************************************************************
function TiEditCustom.GetModified: Boolean;
begin
Result := FUndoText <> FText;
end;
//****************************************************************************************************************************************************
function TiEditCustom.AllowKey(Key: Char): Boolean;
begin
Result := True;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.DeleteCaret;
begin
if FCaretCreated then
begin
{$ifdef iVCL}
HideCaret(Handle);
DestroyCaret;
{$endif}
FCaretCreated := False;
end;
end;
//****************************************************************************************************************************************************
procedure TiEditCustom.iCreateCaret;
{$ifdef iVCL}
var
AHeight : Integer;
{$endif}
begin
if not FCaretCreated then
begin
Canvas.Font.Assign(FFont);
{$ifdef iVCL}
AHeight := Canvas.TextHeight('ABC');
FCaretCreated := CreateCaret(Handle, 0, 0, AHeight);
ShowCaret(Handle);
{$endif}
{$ifdef iCLX}
{$endif}
end;
end;
//****************************************************************************************************************************************************
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -