📄 unumedit.pas
字号:
unit uNumEdit;
interface
uses
Classes, StdCtrls, SysUtils, Messages, Clipbrd, Controls, Types, Windows;
type
TNumEdit = class(TCustomEdit)
private
FAlignment: TAlignment;
FDecCount: Integer;
FIntCount: Integer;
FMaxValue: Double;
FCheckValue: Boolean;
FAllowSign: Boolean;
procedure SetAlignment(Value: TAlignment);
procedure SetIntCount(Value: Integer);
procedure SetDecCount(Value: Integer);
protected
procedure CreateParams(var Params: TCreateParams); override;
published
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
property IntCount: Integer read FIntCount write SetIntCount default 0;
property DecCount: Integer read FDecCount write SetDecCount default 0;
property MaxValue: Double read FMaxValue write FMaxValue;
property CheckValue: Boolean read FCheckValue write FCheckValue default False;
property AllowSign: Boolean read FAllowSign write FAllowSign default False;
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property CharCase;
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property ImeMode;
property ImeName;
property MaxLength;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
public
constructor Create(AOwner: TComponent); override;
procedure DefaultHandler(var Message); override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TNumEdit]);
end;
constructor TNumEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Text := '0';
end;
procedure TNumEdit.DefaultHandler(var Message);
var
S: String;
I, J: Integer;
begin
case TMessage(Message).Msg of
WM_PASTE: begin
S := Copy(Self.Text, 1, Self.SelStart) + ClipBoard.AsText
+ Copy(Self.Text, Self.SelStart + Self.SelLength + 1, 255);
end;
WM_CHAR: begin
if Chr(TMessage(Message).WParam) in [#1, #8, #3, #22] then begin
inherited;
Exit; // BackSpace, Ctrl + C, Ctrl+V
end;
if not (Chr(TMessage(Message).WParam) in ['+', '-', '0'..'9', '.']) then Exit;
S := Copy(Self.Text, 1, Self.SelStart) + Chr(TMessage(Message).WParam)
+ Copy(Self.Text, Self.SelStart + Self.SelLength + 1, 255);
end;
end;
if (TMessage(Message).Msg = WM_PASTE) or (TMessage(Message).Msg = WM_CHAR) then begin
if S[1] = '.' then J := 1 else J := 0;
if (not FAllowSign) and (S[1] in ['+', '-']) then Exit;
for I := 2 to Length(S) do begin
if S[I] = '.' then Inc(J)
else if not (S[I] in ['0'..'9']) then Exit;
end;
if (J > 1) or ((J = 1) and (FDecCount = 0)) then Exit;
if S[1] in ['+', '-'] then S := Copy(S, 2, Length(S));
J := Pos('.', S);
if (J = 0) then J := Length(S) + 1;
if (FIntCount <> 0) and (J > FIntCount + 1) then Exit;
if (FDecCount <> 0) and (Length(S) - J > FDecCount) then Exit;
if (Length(S) > 0) and FCheckValue and (StrToFloat(S) > FMaxValue)
then Exit;
end;
inherited;
end;
procedure TNumEdit.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
procedure TNumEdit.SetIntCount(Value: Integer);
begin
if (Value < 20) and (Value >= 0) then FIntCount := Value
else raise ERangeError.CreateFmt(
'%d is not within the valid range of %d..%d',
[Value, 0, 20]);
end;
procedure TNumEdit.SetDecCount(Value: Integer);
begin
if (Value < 20) and (Value >= 0) then FDecCount := Value
else raise ERangeError.CreateFmt(
'%d is not within the valid range of %d..%d',
[Value, 0, 20]);
end;
procedure TNumEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));
begin
inherited CreateParams(Params);
with Params do
begin
Style := Style or Alignments[UseRightToLeftAlignment, FAlignment];
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -