1 | unit UIPv4Edit;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, StdCtrls, StrUtils;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TIPv4Edit }
|
---|
13 |
|
---|
14 | TIPv4Edit = class(TEdit)
|
---|
15 | private
|
---|
16 | FAddress: Cardinal;
|
---|
17 | procedure ChangeExecute(Sender: TObject);
|
---|
18 | procedure SetAddress(const AValue: Cardinal);
|
---|
19 | public
|
---|
20 | function TryStrToIPv4(S: string; out Value: Cardinal): Boolean;
|
---|
21 | constructor Create(AOwner: TComponent); override;
|
---|
22 | destructor Destroy; override;
|
---|
23 | published
|
---|
24 | property Address: Cardinal read FAddress write SetAddress;
|
---|
25 | end;
|
---|
26 |
|
---|
27 | procedure Register;
|
---|
28 |
|
---|
29 | implementation
|
---|
30 |
|
---|
31 | procedure Register;
|
---|
32 | begin
|
---|
33 | RegisterComponents('TEditExtensions', [TIPv4Edit]);
|
---|
34 | end;
|
---|
35 |
|
---|
36 | { TIPv4Edit }
|
---|
37 |
|
---|
38 | procedure TIPv4Edit.ChangeExecute(Sender: TObject);
|
---|
39 | var
|
---|
40 | NewText: string;
|
---|
41 | Temp: Cardinal;
|
---|
42 | begin
|
---|
43 | NewText := Text;
|
---|
44 | Delete(NewText, SelStart + 1, 1);
|
---|
45 | if TryStrToIPv4(NewText, Temp) then
|
---|
46 | FAddress := Temp;
|
---|
47 | SetAddress(FAddress);
|
---|
48 | end;
|
---|
49 |
|
---|
50 | procedure TIPv4Edit.SetAddress(const AValue: Cardinal);
|
---|
51 | var
|
---|
52 | LastPos: Integer;
|
---|
53 | Adr: string;
|
---|
54 | I: Integer;
|
---|
55 | Octet: string;
|
---|
56 | begin
|
---|
57 | FAddress := AValue;
|
---|
58 | LastPos := SelStart;
|
---|
59 | OnChange := nil;
|
---|
60 | Adr := '';
|
---|
61 | for I := 0 to 3 do begin
|
---|
62 | Octet := IntToStr((FAddress shr (24 - (I * 8))) and $ff);
|
---|
63 | Adr := Adr + Octet + DupeString(' ', 3 - Length(Octet));
|
---|
64 | if I < 3 then Adr := Adr + '.';
|
---|
65 | end;
|
---|
66 | Text := Adr;
|
---|
67 | OnChange := ChangeExecute;
|
---|
68 | if LastPos = 3 then LastPos := 4;
|
---|
69 | if LastPos = 7 then LastPos := 8;
|
---|
70 | if LastPos = 11 then LastPos := 12;
|
---|
71 | SelStart := LastPos;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | function TIPv4Edit.TryStrToIPv4(S: string; out Value: Cardinal): Boolean;
|
---|
75 | var
|
---|
76 | P: Integer;
|
---|
77 | Octet: Integer;
|
---|
78 | OctetText: string;
|
---|
79 | I: Integer;
|
---|
80 | begin
|
---|
81 | Result := False;
|
---|
82 | Value := 0;
|
---|
83 | for I := 0 to 3 do begin
|
---|
84 | if I < 3 then begin
|
---|
85 | P := Pos('.', S);
|
---|
86 | if P > 0 then
|
---|
87 | OctetText := Copy(S, 1, P - 1) else Exit;
|
---|
88 | Delete(S, 1, P);
|
---|
89 | end else OctetText := S;
|
---|
90 | if TryStrToInt(Trim(OctetText), Octet) then begin
|
---|
91 | if (Octet >= 0) and (Octet <= 255) then
|
---|
92 | Value := Value or (Byte(Octet) shl (24 - (I * 8)))
|
---|
93 | else Exit;
|
---|
94 | end else Exit;
|
---|
95 | end;
|
---|
96 | Result := True;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | constructor TIPv4Edit.Create(AOwner: TComponent);
|
---|
100 | begin
|
---|
101 | inherited Create(AOwner);
|
---|
102 | OnChange := ChangeExecute;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | destructor TIPv4Edit.Destroy;
|
---|
106 | begin
|
---|
107 | inherited Destroy;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | end.
|
---|
111 |
|
---|