📄 ipfieldedit.pas
字号:
unit IPFieldEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TIPFieldEdit = class(TEdit)
private
{ Private declarations }
FError: Boolean;
FMin, FMax: BYTE;
FValue: BYTE;
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure KeyPress(var Key: Char); override;
procedure TextChanged(Sender: TObject);
procedure WMKeyDown(var Message: TWMKEy); message WM_KEYDOWN;
procedure SetMin(value: Byte);
procedure SetMax(value: Byte);
procedure SetValue(value: Byte);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetCurrentPosition: integer;
procedure SetCurrentPosition(Value: integer);
property CurrentPosition: integer read GetCurrentPosition write SetCurrentPosition;
property Value: Byte read FValue write SetValue;
property Error: Boolean read FError;
property Min: Byte read FMin write SetMin default 0;
property Max: Byte read FMax write SetMax default 255;
property ReadOnly stored False;
published
{ Published declarations }
end;
implementation
{ TIPFieldEdit }
uses IPEdit;
procedure TIPFieldEdit.SetMin(value: Byte);
begin
FMin := value;
if FMax < FMin then
FMax := FMin;
end;
procedure TIPFieldEdit.SetMax(value: Byte);
begin
FMax := value;
if FMin > FMax then
FMin := FMax;
end;
procedure TIPFieldEdit.SetValue(value: Byte);
begin
FValue := value;
Text := inttostr(value)
end;
procedure TIPFieldEdit.KeyPress(var Key: Char);
begin
if (Key >= '0') and (Key <= '9') then
begin
inherited;
end
else
begin
if (Key = '.') and (SelLength = 0) and (Text <> '') then
(Parent as TIPEdit).ActiveNextField(True);
if Key <> #8 then
Key := #0
else if CurrentPosition = 0 then
(Parent as TIPEdit).ActivePrevField;
end;
end;
procedure TIPFieldEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or (ES_CENTER or Es_MULTILINE);
end;
constructor TIPFieldEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FError := False;
Text := '0';
FMin := 0;
FMax := 255;
MaxLength := 3;
ParentFont := True;
ParentColor := True;
BorderStyle := bsNone;
OnChange := TextChanged;
end;
destructor TIPFieldEdit.Destroy;
begin
inherited Destroy;
end;
procedure TIPFieldEdit.TextChanged(Sender: TObject);
var
v, er: integer;
begin
inherited;
if Text <> '' then
begin
Val(Text, v, er);
if (er <> 0) or (v < FMin) or (v > FMax) then
begin
FError := True;
with (Parent as TIPEdit) do
begin
if Assigned(OnError) then
OnError(Parent, Self.TabOrder);
Text:='255';//赵明达 2002.07.16
end;
end
else
begin
FError := False;
if (FValue <> v) then
begin
FValue := v;
with (Parent as TIPEdit) do
if Assigned(OnChange) then
OnChange(Parent);
end;
end;
end;
if not FError and (Length(Text) = 3) and (CurrentPosition = 3) then
(Parent as TIPEdit).ActiveNextField(True);
end;
function TIPFieldEdit.GetCurrentPosition: integer;
{Get character position of cursor within line}
begin
result := SelStart - SendMessage(Handle, EM_LINEINDEX,
(SendMessage(Handle, EM_LINEFROMCHAR, SelStart, 0)), 0);
end;
procedure TIPFieldEdit.SetCurrentPosition(Value: integer);
var
curposn: integer;
begin
{Value must be within range}
curposn := Value;
if curposn < 0 then
curposn := 0;
if curposn > Length(Text) then
curposn := Length(Text);
{Put cursor in selected position}
SelStart := SendMessage(Handle, EM_LINEINDEX, 0, 0) + curposn;
end;
procedure TIPFieldEdit.WMKeyDown(var Message: TWMKey);
begin
with Message do
if (CharCode = VK_RIGHT)
and (CurrentPosition >= Length(Text)) then
begin
SelLength := 0;
(Parent as TIPEdit).ActiveNextField;
Result := 1;
end
else if (CharCode = VK_LEFT)
and (CurrentPosition = 0) then
begin
SelLength := 0;
(Parent as TIPEdit).ActivePrevField;
Result := 1;
end
else
inherited;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -