这是一个:用COMMCTRL做的,所以要安装IE4的机器才行:
注意自己添加Register函数。
////////////////////////////////////////////////////////////////////////////////
// IPADRESS98 //
////////////////////////////////////////////////////////////////////////////////
// An implementation of IE4's IPADDRESS Control //
////////////////////////////////////////////////////////////////////////////////
// Version 1.00 Beta //
// Date de cr閍tion : 20/10/1997 //
// Date derni鑢e modification : 21/10/1997 //
////////////////////////////////////////////////////////////////////////////////
// Jean-Luc Mattei //
// jlucm@club-internet.fr / jlucm@mygale.org //
////////////////////////////////////////////////////////////////////////////////
// IMPORTANT NOTICE : //
// //
// //
// This program is FreeWare //
// //
// Please do not release modified versions of this source code. //
// If you've made any changes that you think should have been there, //
// feel free to submit them to me at jlucm@club-internet.fr //
////////////////////////////////////////////////////////////////////////////////
// REVISIONS : //
// //
////////////////////////////////////////////////////////////////////////////////
unit IpAdress98;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, CommCtrl, Comctl98;
type
TCustomIPAdress98 = class;
TIPAdressFieldChangeEvent = procedure (Sender: TCustomIPAdress98; OldField, Value: Byte) of object;
TIPAdressChangeEvent = procedure (Sender: TCustomIPAdress98; IPAdress: String) of object;
TCustomIPAdress98 = class(TWinControl)
private
FOnIPChange: TIPAdressChangeEvent;
FOnIPFieldChange: TIPAdressFieldChangeEvent;
FMinIPAdress: Longint;
FMaxIPAdress: Longint;
FActiveField: Byte;
FAutoSize: Boolean;
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
protected
procedure CreateParams(var Params: TCreateParams); override;
function GetMinIPAdress: String;
function GetMaxIPAdress: String;
function GetIPAdress: String;
procedure SetMinIPAdress(Value: String);
procedure SetMaxIPAdress(Value: String);
procedure SetIPAdress(Value: String);
function GetEmpty: Boolean;
procedure SetActiveField(Value: Byte);
public
constructor Create(AOwner: TComponent); override;
function IPToString(Ip: Longint): String;
function StringToIP(Value: String): Longint;
procedure Clear;
property ActiveField: Byte read FActiveField write SetActiveField;
property Empty: Boolean read GetEmpty;
property MinIPAdress: String read GetMinIPAdress write SetMinIPAdress;
property MaxIPAdress: String read GetMaxIPAdress write SetMaxIPAdress;
property IPAdress: String read GetIPAdress write SetIPAdress;
property OnIPChange: TIPAdressChangeEvent read FOnIPChange write FOnIPChange;
property OnIPFieldChange: TIPAdressFieldChangeEvent read FOnIPFieldChange write FOnIPFieldChange;
end;
TIPAdress98 = class(TCustomIPAdress98)
published
property ActiveField;
property Empty;
property MinIPAdress;
property MaxIPAdress;
property IPAdress;
property OnIPChange;
property OnIPFieldChange;
property Font;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Tag;
property DragCursor;
property DragMode;
property HelpContext;
end;
implementation
constructor TCustomIPAdress98.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if NewStyleControls then
ControlStyle := [csClickEvents, csSetCaption, csDoubleClicks, csFixedHeight]
else
ControlStyle := [csClickEvents, csSetCaption, csDoubleClicks, csFixedHeight, csFramed];
ParentColor := False;
FAutoSize := True;
Width:= 100;
Height:= 25;
TabStop:= True;
FMinIPAdress:= 0;
{$WARNINGS OFF}
FMaxIPAdress:= $0FFFFFFFF;
{$WARNINGS ON}
FActiveField:= 0;
FOnIPChange:= nil;
FOnIPFieldChange:= nil;
end;
procedure TCustomIPAdress98.CreateParams(var Params: TCreateParams);
begin
InitCommonControl(ICC_INTERNET_CLASSES);
inherited CreateParams(Params);
CreateSubClass(Params, WC_IPADDRESS);
with Params do
begin
Style := WS_VISIBLE or WS_BORDER or WS_CHILD;
if NewStyleControls and Ctl3D then
begin
Style := Style and not WS_BORDER;
ExStyle := ExStyle or WS_EX_CLIENTEDGE;
end;
end;
end;
procedure TCustomIPAdress98.CNNotify(var Message: TWMNotify);
begin
with Message.NMHdr^ do begin
case Code of
IPN_FIELDCHANGED :
begin
FActiveField:= PNMIPAdress(Message.NMHdr)^.iField;
if Assigned(OnIpFieldChange) then
with PNMIPAdress(Message.NMHdr)^ do begin
OnIPFieldChange(Self, iField, iValue);
end;
end;
end;
end;
end;
function TCustomIPAdress98.GetIPAdress: String;
var Ip: Longint;
begin
SendMessage(Handle, IPM_GETADDRESS, 0, Longint(@Ip));
Result:= IPToString(Ip);
end;
function TCustomIPAdress98.GetMinIPAdress: String;
begin
Result:= IPToString(FMinIPAdress);
end;
procedure TCustomIPAdress98.SetMinIPAdress(Value: String);
begin
FMinIPAdress:= StringToIp(Value);
SendMessage(Handle, IPM_SETRANGE, 0, MakeIpRange(First_IPAdress(FMinIPAdress), First_IPAdress(FMaxIPAdress)));
SendMessage(Handle, IPM_SETRANGE, 1, MakeIpRange(Second_IPAdress(FMinIPAdress), Second_IPAdress(FMaxIPAdress)));
SendMessage(Handle, IPM_SETRANGE, 2, MakeIpRange(Third_IPAdress(FMinIPAdress), Third_IPAdress(FMaxIPAdress)));
SendMessage(Handle, IPM_SETRANGE, 3, MakeIpRange(Fourth_IPAdress(FMinIPAdress), Fourth_IPAdress(FMaxIPAdress)));
end;
function TCustomIPAdress98.GetMaxIPAdress: String;
begin
Result:= IPToString(FMaxIPAdress);
end;
procedure TCustomIPAdress98.SetMaxIPAdress(Value: String);
begin
FMaxIPAdress:= StringToIp(Value);
SendMessage(Handle, IPM_SETRANGE, 0, MakeIpRange(First_IPAdress(FMinIPAdress), First_IPAdress(FMaxIPAdress)));
SendMessage(Handle, IPM_SETRANGE, 1, MakeIpRange(Second_IPAdress(FMinIPAdress), Second_IPAdress(FMaxIPAdress)));
SendMessage(Handle, IPM_SETRANGE, 2, MakeIpRange(Third_IPAdress(FMinIPAdress), Third_IPAdress(FMaxIPAdress)));
SendMessage(Handle, IPM_SETRANGE, 3, MakeIpRange(Fourth_IPAdress(FMinIPAdress), Fourth_IPAdress(FMaxIPAdress)));
end;
procedure TCustomIPAdress98.SetIPAdress(Value: String);
begin
SendMessage(Handle, IPM_SETADDRESS, 0, StringToIp(Value));
end;
function TCustomIPAdress98.GetEmpty: Boolean;
begin
Result:= Boolean(SendMessage(Handle, IPM_ISBLANK, 0, 0));
end;
procedure TCustomIPAdress98.Clear;
begin
SendMessage(Handle, IPM_CLEARADDRESS, 0, 0);
end;
procedure TCustomIPAdress98.SetActiveField(Value: Byte);
begin
if ( Value < 4 ) then begin
SendMessage(Handle, IPM_SETFOCUS, wParam(Value), 0);
FActiveField:= Value;
end;
end;
function TCustomIPAdress98.StringToIp(Value: String): Longint;
var B: Array[0..3] of Byte;
Str: String;
i, Cnt : Integer;
begin
B[0]:= 0;
B[1]:= 0;
B[2]:= 0;
B[3]:= 0;
Cnt:= 0;
i:= Pos('.', Value);
while (Length(Value) > 0) and ( Cnt < 4 ) do begin
if ( i = 0 ) then i:= Length(Value)+1;
Str:= Copy(Value, 0, i-1);
B[Cnt]:= StrToInt(Str);
Value:= Copy(Value, i+1, Length(Value));
i:= Pos('.', Value);
Inc(Cnt);
end;
Result:= MakeIPAdress(b[0], b[1], b[2], b[3]);
end;
function TCustomIPAdress98.IPToString(Ip: Longint): String;
begin
Result:= IntToStr(First_IPAdress(Ip))+'.'+IntToStr(Second_IPAdress(Ip))+'.'+
IntToStr(Third_IPAdress(Ip))+'.'+IntToStr(Fourth_IPAdress(Ip));
end;
procedure TCustomIPAdress98.CNCommand(var Message: TWMCommand);
begin
if (Message.NotifyCode = EN_CHANGE) and Assigned(OnIpChange) then
OnIPChange(Self, IPAdress);
end;
end.