{~~~~~~~~~~~~~~~~~~~~~~TIPEdit~~~~~~~~~~~~~~~~~~~~~~~~}
constructor TIPEdit.Create(AOwner: TComponent);
var
i: integer;
begin
//初始化ICC_INTERNET_CLASSES类控件
CheckCommonControl(ICC_INTERNET_CLASSES);
inherited Create(AOwner);
for i:= 0 to 3 do
begin
FFieldRanges.LowRange:= 0;
FFieldRanges.HighRange:= 255;
FFields:=0;
end;
FIP:=0;
FIPAddress := '0.0.0.0';
Height:= 25;
Width:= 152;
TabSTop:= True;
end;
procedure TIPEdit.DestroyWnd;
begin
inherited DestroyWnd
end;
destructor TIPEdit.Destroy;
begin
inherited Destroy;
end;
procedure TIPEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
CreateSubClass(Params, WC_IPADDRESS);
with Params do
begin
end;
end;
function TIPEdit.GetBlank: Boolean;
begin
Result:= Boolean(SendMessage(Handle, IPM_ISBLANK, 0, 0));
end;
procedure TIPEdit.Clear;
begin
SendMessage(Handle, IPM_CLEARADDRESS, 0, 0);
end;
procedure TIPEdit.SetFieldFocus(Field:Byte);
begin
SendMessage(Handle, IPM_SETFOCUS, Field, 0);
end;
function TIPEdit.GetFieldRange(Field: Integer): TFieldRange;
begin
Result:= FFieldRanges[Field];
end;
procedure TIPEdit.SetFieldRange(Field: Integer; Value: TFieldRange);
begin
if Value.LowRange > Value.HighRange then exit;
if (FFieldRanges[Field].LowRange <> Value.LowRange) or
(FFieldRanges[Field].HighRange <> Value.HighRange) then
begin
FFieldRanges[Field]:= Value;
SendMessage(Handle, IPM_SETRANGE, Field,
MakeIPRange(Value.LowRange, Value.HighRange));
end;
end;
function TIPEdit.GetField(Index: Integer): Byte;
begin
if (Index>=0)and(Index<=3) then Result:=FFields[Index]
else Result:=0;
end;
procedure TIPEdit.SetField(Index:Integer; B:Byte);
begin
if (FFields[Index] <> B)then
begin
FFields[Index]:=B;
SetIPAddress;
end;
end;
procedure TIPEdit.SetIPAddress;
var
i:LongWord;
begin
i:=MAKEIPADDRESS(FFields[0], FFields[1], FFields[2], FFields[3]);
SendMessage(Handle, IPM_SETADDRESS, 0, i);
FIP:=i;
end;
procedure TIPEdit.SetIP(Value: LongWord);
begin
if (FIP <> Value) then
begin
FFields[0]:= First_IPAddress(Value);
FFields[1]:= Second_IPAddress(Value);
FFields[2]:= Third_IPAddress(Value);
FFields[3]:= Fourth_IPAddress(Value);
SetIPAddress;
end;
end;
function TIPEdit.GetIP: LongWord;
begin
SendMessage(Handle, IPM_GETADDRESS, 0, Integer(@Result));
end;
procedure TIPEdit.WMNotifyFormat(var Message: TMessage);
begin
with Message do
Result := DefWindowProc(Handle, Msg, WParam, LParam);
end;
procedure TIPEdit.CNNotify(var Message: TWMNotify);
var
pNM: PNMIPAddress;
begin
with (Message.NMHdr)^ do
begin
case Code of
IPN_FIELDCHANGED:
begin
pNM:= PNMIPADDRESS(Message.NMHdr);
if (pNM^.iField>=0)and(pNM^.iField<=3) then
FFields[pNM^.iField]:=pNM^.iValue;
if Assigned(FOnFieldChange) then
FOnFieldChange(self, pNM^.iField, pNM^.iValue);
end;
end;
end;
end;
procedure TIPEdit.CNCommand(var Message: TWMCommand);
begin
case Message.NotifyCode of
EN_CHANGE:
begin
if not FCreating then
if Assigned(FOnChange) then FOnChange(self);
end;
EN_KILLFOCUS: if Assigned(FOnExit) then FOnExit(self);
EN_SETFOCUS: if Assigned(FOnEnter) then FOnEnter(self);
end;
end;
// 新增,设置新的 IP 地址
procedure TIpEdit.SetIPString(Value: string);
// 转化'a.a.a.a'的字段为32为整数
function IP2Int(Value: string): Integer;
var
I: Integer;
strs: TStrings;
begin
strs := TStringList.Create;
Value := StringReplace(Value, '.', ',', [rfReplaceAll]);
strs.CommaText := Value;
Result := 0;
try
for I := 0 to strs.Count - 1 do // Iterate
begin // 2130706433
Result := Result shl 8 + StrToInt(strs);
end; // for
except
Result := -1;
Application.MessageBox('地址格式错误', nil, MB_OK + MB_ICONERROR);
end;
strs.Free;
end;
var
i: Integer;
begin
if FIPAddress <> Value then
begin
i := IP2Int(Value);
if i <> -1 then
begin
FIPAddress := Value;
FFields[0]:= First_IPAddress(i);
FFields[1]:= Second_IPAddress(i);
FFields[2]:= Third_IPAddress(i);
FFields[3]:= Fourth_IPAddress(i);
SetIPAddress;
end;
end;
end;
// 新增,获取新的 IP 地址
function TIpEdit.GetIPString: string;
begin
FIPAddress := Format('%d.%d.%d.%d', [FFields[0], FFields[1], FFields[2], FFields[3]]);
Result := FIpAddress;
end;
procedure Register;
begin
RegisterComponents('Liren.z', [TIPEdit]);
end;