请问那里有输入IP地址的控件(用代码实现也行)?(50分)

  • 主题发起人 主题发起人 lipingcool
  • 开始时间 开始时间
L

lipingcool

Unregistered / Unconfirmed
GUEST, unregistred user!
同样的问题我在大富翁里已经看到过,但是所说的代码放在程序里不能编
译,也不知是不是没有uses什么东西.有这样的控件吗?
深度历险我老是连不上去,也不知道上面是不是有这样的控件.
 
我也想知道!
 
msdn中谈到有个IP address control,我一直不知道什么把它注册到DELPHI中。
The IP address control is implemented in version 4.71 and later of Comctl32.dll.
老兄如果搞出来的话也告诉我一声。

 
我也想知道
 
那位朋友有IP控件,以来控制IP地址的输入,本人出50低分索取。
http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=202869
 
我这有这个控件的源代码:
你自己去按装吧!
////////////////////////////////////////////////////////////////////////////////
// IPADRESS98 //
////////////////////////////////////////////////////////////////////////////////
unit IPAddress;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, CommCtrl;

type

TCustomIPAddress98 = class;

TIPAddressFieldChangeEvent = procedure (Sender: TCustomIPAddress98; OldField, Value: Byte) of object;
TIPAddressChangeEvent = procedure (Sender: TCustomIPAddress98; IPAddress: String) of object;

TCustomIPAddress98 = class(TWinControl)
private
FOnIPChange: TIPAddressChangeEvent;
FOnIPFieldChange: TIPAddressFieldChangeEvent;
FMinIPAddress: Longint;
FMaxIPAddress: 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 GetMinIPAddress: String;
function GetMaxIPAddress: String;
function GetIPAddress: String;
procedure SetMinIPAddress(Value: String);
procedure SetMaxIPAddress(Value: String);
procedure SetIPAddress(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 MinIPAddress: String read GetMinIPAddress write SetMinIPAddress;
property MaxIPAddress: String read GetMaxIPAddress write SetMaxIPAddress;
property IPAddress: String read GetIPAddress write SetIPAddress;
property OnIPChange: TIPAddressChangeEvent read FOnIPChange write FOnIPChange;
property OnIPFieldChange: TIPAddressFieldChangeEvent read FOnIPFieldChange write FOnIPFieldChange;
end;

TIPAddress98 = class(TCustomIPAddress98)
published
property ActiveField;
property Empty;
property MinIPAddress;
property MaxIPAddress;
property IPAddress;
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;
procedure Register;
implementation

procedure Register;
begin
RegisterComponents('Samples', [TIPAddress98]);
end;

constructor TCustomIPAddress98.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;
FMinIPAddress:= 0;
{$WARNINGS OFF}
FMaxIPAddress:= $0FFFFFFFF;
{$WARNINGS ON}
FActiveField:= 0;
FOnIPChange:= nil;
FOnIPFieldChange:= nil;
end;

procedure TCustomIPAddress98.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 TCustomIPAddress98.CNNotify(var Message: TWMNotify);
begin
with Message.NMHdr^ do begin
case Code of
IPN_FIELDCHANGED :
begin
FActiveField:= PNMIPAddress(Message.NMHdr)^.iField;
if Assigned(OnIpFieldChange) then
with PNMIPAddress(Message.NMHdr)^ do begin
OnIPFieldChange(Self, iField, iValue);
end;
end;
end;
end;
end;

function TCustomIPAddress98.GetIPAddress: String;
var Ip: Longint;
begin
SendMessage(Handle, IPM_GETADDRESS, 0, Longint(@Ip));
Result:= IPToString(Ip);
end;

function TCustomIPAddress98.GetMinIPAddress: String;
begin
Result:= IPToString(FMinIPAddress);
end;

procedure TCustomIPAddress98.SetMinIPAddress(Value: String);
begin
FMinIPAddress:= StringToIp(Value);
SendMessage(Handle, IPM_SETRANGE, 0, MakeIpRange(First_IPAddress(FMinIPAddress), First_IPAddress(FMaxIPAddress)));
SendMessage(Handle, IPM_SETRANGE, 1, MakeIpRange(Second_IPAddress(FMinIPAddress), Second_IPAddress(FMaxIPAddress)));
SendMessage(Handle, IPM_SETRANGE, 2, MakeIpRange(Third_IPAddress(FMinIPAddress), Third_IPAddress(FMaxIPAddress)));
SendMessage(Handle, IPM_SETRANGE, 3, MakeIpRange(Fourth_IPAddress(FMinIPAddress), Fourth_IPAddress(FMaxIPAddress)));
end;

function TCustomIPAddress98.GetMaxIPAddress: String;
begin
Result:= IPToString(FMaxIPAddress);
end;

procedure TCustomIPAddress98.SetMaxIPAddress(Value: String);
begin
FMaxIPAddress:= StringToIp(Value);
SendMessage(Handle, IPM_SETRANGE, 0, MakeIpRange(First_IPAddress(FMinIPAddress), First_IPAddress(FMaxIPAddress)));
SendMessage(Handle, IPM_SETRANGE, 1, MakeIpRange(Second_IPAddress(FMinIPAddress), Second_IPAddress(FMaxIPAddress)));
SendMessage(Handle, IPM_SETRANGE, 2, MakeIpRange(Third_IPAddress(FMinIPAddress), Third_IPAddress(FMaxIPAddress)));
SendMessage(Handle, IPM_SETRANGE, 3, MakeIpRange(Fourth_IPAddress(FMinIPAddress), Fourth_IPAddress(FMaxIPAddress)));
end;

procedure TCustomIPAddress98.SetIPAddress(Value: String);
begin
SendMessage(Handle, IPM_SETADDRESS, 0, StringToIp(Value));
end;

function TCustomIPAddress98.GetEmpty: Boolean;
begin
Result:= Boolean(SendMessage(Handle, IPM_ISBLANK, 0, 0));
end;

procedure TCustomIPAddress98.Clear;
begin
SendMessage(Handle, IPM_CLEARADDRESS, 0, 0);
end;

procedure TCustomIPAddress98.SetActiveField(Value: Byte);
begin
if ( Value < 4 ) then begin
SendMessage(Handle, IPM_SETFOCUS, wParam(Value), 0);
FActiveField:= Value;
end;
end;

function TCustomIPAddress98.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:= MakeIPAddress(b[0], b[1], b[2], b[3]);
end;

function TCustomIPAddress98.IPToString(Ip: Longint): String;
begin
Result:= IntToStr(First_IPAddress(Ip))+'.'+IntToStr(Second_IPAddress(Ip))+'.'+
IntToStr(Third_IPAddress(Ip))+'.'+IntToStr(Fourth_IPAddress(Ip));
end;

procedure TCustomIPAddress98.CNCommand(var Message: TWMCommand);
begin
if (Message.NotifyCode = EN_CHANGE) and Assigned(OnIpChange) then
OnIPChange(Self, IPAddress);
end;

end.


 
其实这类控件 windows 有, delphi 中也有一点点,在
commctrl.pas 中,但我看不动懂, 谁能解释一下?
 
李崇文:
我用你给的源代码把输入IP地址的组件装上去了,还比较好用,但是放在我的程序中运行的话,会造成"访问无效地址"的错误,你知道怎么解决吗?多谢!
 
李崇文:
我用你给的源代码把输入IP地址的组件装上去了,但是放在我的程序中运行的话,
会造成"访问无效地址"的错误以及窗体出现乱码现象,你知道怎么解决吗?多谢!
 
下这文章来看看吧,计算机世界上的文章。
http://www.ccw.com.cn/99/tips/2k05/052602-05p.zip
 
unit CXIPAddress;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs
,commctrl,comctrls;

type

TFieldRange = record
LowRange: Byte;
HighRange: Byte;
end;

TphFieldChange = procedure(Sender: TObject; Field: Integer;
Value: Integer) of object;

TCXIPAddress = class(TWinControl)
private
{ Private declarations }
// FFont: TFont;
FActiveField:Byte;
FCreating: Boolean;
FValue: LongInt;
FFieldRanges: array [0..3] of TFieldRange;
FOnChange: TNotifyEvent;
FOnEnter: TNotifyEvent;
FOnExit: TNotifyEvent;
FOnFieldChange: TphFieldChange;
FFirstIPAddress: Byte;
FSecondIPAddress: Byte;
FThirdIPAddress: Byte;
FFourthIPAddress: Byte;
procedure SetActiveField(Value:Byte);
function GetFirstIPAddress: Byte;
procedure SetFirstIPAddress(Value: Byte);
function GetSecondIPAddress: Byte;
procedure SetSecondIPAddress(Value: Byte);
function GetThirdIPAddress: Byte;
procedure SetThirdIPAddress(Value: Byte);
function GetFourthIPAddress: Byte;
procedure SetFourthIPAddress(Value: Byte);
function GetIPAddress: LongInt;
procedure SetIPAddress(Value:Longint);
function GetBlank: Boolean;
function GetFieldRanges(Field: Integer): TFieldRange;
procedure SetFieldRanges(Field:integer; Value: TFieldRange);
//procedure SetValue(Value: Cardinal);
//function GetValue: LongInt;
//procedure WMNotifyFormat(var Message: TMessage); message WM_NOTIFYFORMAT;
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
//procedure WMSetFont(var Message: TWMSetFont); message WM_SETFONT;
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateWnd; override;
// procedure DestroyWnd; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
//destructor Destroy; override;
procedure ClearAddress;
function StringToIp(Value: String): Longint;
function IPToString(Ip: Longint): string;
procedure SetAllRanges(Range: TFieldRange);
property IPAddress: Longint read GetIPAddress write SetIPAddress;
published
{ Published declarations }
property ActiveField: Byte read FActiveField write SetActiveField;
property Blank: Boolean read GetBlank;
property FirstRange: TFieldRange index 0 read GetFieldRanges write SetFieldRanges;
property SecondRange: TFieldRange index 1 read GetFieldRanges write SetFieldRanges;
property ThirdRange: TFieldRange index 2 read GetFieldRanges write SetFieldRanges;
property FourthRange: TFieldRange index 3 read GetFieldRanges write SetFieldRanges;
property Field0: Byte read GetFirstIPAddress write SetFirstIPAddress;
property Field1: Byte read GetSecondIPAddress write SetSecondIPAddress;
property Field2: Byte read GetThirdIPAddress write SetThirdIPAddress;
property Field3: Byte read GetFourthIPAddress write SetFourthIPAddress;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
property OnExit: TNotifyEvent read FOnExit write FOnExit;
property OnFieldChange: TphFieldChange read FOnFieldChange write FOnFieldChange;
property Font;
property Enabled;
property TabOrder;
property TabStop;
property ParentShowHint;
property ShowHint;
property Hint;
property PopupMenu;
property Visible;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('CXLib', [TCXIPAddress]);
end;

{ TCXIPAddress }

procedure TCXIPAddress.ClearAddress;
begin
Perform(IPM_CLEARADDRESS, 0, 0);
end;

procedure TCXIPAddress.CNCommand(var Message: TWMCommand);
begin
case Message.NotifyCode of
EN_CHANGE:
if not FCreating then
if Assigned(FOnChange) then FOnChange(self);
EN_KILLFOCUS:
if Assigned(FOnExit) then FOnExit(self);
EN_SETFOCUS:
if Assigned(FOnEnter) then FOnEnter(self);
end;
end;

procedure TCXIPAddress.CNNotify(var Message: TWMNotify);
var
IPAddress: PNMIPADDRESS;
begin
with (Message.NMHdr)^ do
begin
case Code of
IPN_FIELDCHANGED:
begin
IPAddress:= PNMIPADDRESS(Message.NMHdr);
FActiveField:=IPAddress^.iField;
case IPAddress^.iField of
0: FFirstIPAddress := IPAddress^.iValue;
1: FSecondIPAddress := IPAddress^.iValue;
2: FThirdIPAddress := IPAddress^.iValue;
3: FFourthIPAddress := IPAddress^.iValue;
end;
if Assigned(FOnFieldChange) then FOnFieldChange(self,
IPAddress^.iField, IPAddress^.iValue);
end;
end;
end;
end;

constructor TCXIPAddress.Create(AOwner: TComponent);
var
i: integer;
begin
CheckCommonControl(ICC_INTERNET_CLASSES or ICC_BAR_CLASSES);
inherited Create(AOwner);
//ControlStyle := [csClickEvents, csCaptureMouse];

For i:= 0 to 3 do
begin
FFieldRanges.LowRange:= 0;
FFieldRanges.HighRange:= 255;
end;

FFirstIPAddress:= 0;
FSecondIPAddress:= 0;
FThirdIPAddress:= 0;
FFourthIPAddress:= 0;

FValue:= MakeIPAddress(FFirstIPAddress, FSecondIPAddress,
FThirdIPAddress, FFourthIPAddress);

Height:= 25;
Width:= 151;

TabSTop:= True;
end;

procedure TCXIPAddress.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
CreateSubClass(Params, WC_IPADDRESS);
end;

procedure TCXIPAddress.CreateWnd;
begin
FCreating := True;
try
inherited CreateWnd;
//set the IPAddress to 0.0.0.0
SetIPAddress(FValue);
finally
FCreating := False;
end;
end;

{destructor TCXIPAddress.Destroy;
begin
inherited ;
end;}

{procedure TCXIPAddress.DestroyWnd;
begin
inherited;
end;}



function TCXIPAddress.GetBlank: Boolean;
begin
Result:= Boolean(perform(IPM_ISBLANK, 0, 0));
end;

function TCXIPAddress.GetFieldRanges(Field: Integer): TFieldRange;
begin
Result:= FFieldRanges[Field];
end;

function TCXIPAddress.GetFirstIPAddress: Byte;
begin
Result:= FIRST_IPADDRESS(IPAddress);//GetIPAddress(0);
end;

function TCXIPAddress.GetFourthIPAddress: Byte;
begin
Result:= FOURTH_IPADDRESS(IPAddress);//GetIPAddress(3);
end;

function TCXIPAddress.GetIPAddress: LongInt;
var
buffer: LongInt;
begin
buffer:= 0;
perform(IPM_GETADDRESS, 0, LongInt(@Buffer));
Result:= Buffer;
end;

function TCXIPAddress.GetSecondIPAddress: Byte;
begin
Result:= SECOND_IPADDRESS(IPAddress);//GetIPAddress(1);
end;

function TCXIPAddress.GetThirdIPAddress: Byte;
begin
Result:=THIRD_IPADDRESS(IPAddress); //GetIPAddress(2);
end;

procedure TCXIPAddress.SetActiveField(Value: Byte);
begin
if FActiveField=Value then Exit;
if ( Value < 4 ) then begin
SendMessage(Handle, IPM_SETFOCUS, wParam(Value), 0);
FActiveField:= Value;
end;
end;

procedure TCXIPAddress.SetAllRanges(Range: TFieldRange);
var
I:integer;
begin
for i:= 0 to 3 do
begin
SetFieldRanges(i, Range);
end;
end;

procedure TCXIPAddress.SetFieldRanges(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;
perform(IPM_SETRANGE, Field,
MakeIPRange(Value.LowRange, Value.HighRange));
end;
end;

procedure TCXIPAddress.SetFirstIPAddress(Value: Byte);
begin
if (FFirstIPAddress <> Value) then
begin
FFirstIPAddress:= Value;
IPAddress:=MAKEIPADDRESS(FFirstIPAddress, FSecondIPAddress,
FThirdIPAddress, FFourthIPAddress);
end;
end;

procedure TCXIPAddress.SetFourthIPAddress(Value: Byte);
begin
if (FFourthIPAddress <> Value) then
begin
FFourthIPAddress:= Value;
IPAddress:=MAKEIPADDRESS(FFirstIPAddress, FSecondIPAddress,
FThirdIPAddress, FFourthIPAddress);//SetIPAddress;
end;
end;

procedure TCXIPAddress.SetIPAddress(Value:Longint);
begin
if FValue=Value then Exit;
Perform(IPM_SETADDRESS, 0,
MAKEIPADDRESS(FFirstIPAddress, FSecondIPAddress,
FThirdIPAddress, FFourthIPAddress));
Perform(IPM_GETADDRESS, 0, Longint(@FValue));
end;

procedure TCXIPAddress.SetSecondIPAddress(Value: Byte);
begin
if (FSecondIPAddress <> Value) then
begin
FSecondIPAddress:= Value;
IPAddress:=MAKEIPADDRESS(FFirstIPAddress, FSecondIPAddress,
FThirdIPAddress, FFourthIPAddress);//SetIPAddress;
end;
end;

procedure TCXIPAddress.SetThirdIPAddress(Value: Byte);
begin
if (FThirdIPAddress <> Value) then
begin
FThirdIPAddress:= Value;
IPAddress:=MAKEIPADDRESS(FFirstIPAddress, FSecondIPAddress,
FThirdIPAddress, FFourthIPAddress);//SetIPAddress(;
end;
end;

function TCXIPAddress.IPToString(Ip: Longint):string;
begin
Result:= IntToStr(First_IPAddress(Ip))+'.'+IntToStr(Second_IPAddress(Ip))+'.'+
IntToStr(Third_IPAddress(Ip))+'.'+IntToStr(Fourth_IPAddress(Ip));
end;

function TCXIPAddress.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:= MakeIPAddress(b[0], b[1], b[2], b[3]);
end;

end.
//改进ipaddress变成ip,增加字符串形式的ipAddress.

试试我写的这个IPAddress控件如何:)
 
我这有获得IP地址的代码
struct hostent *thisHost;
struct in_addr in;
char MyName[80];
char *IP,*ptr;

if(gethostname (MyName,80)==SOCKET_ERROR)
return NULL;
if(!(thisHost = gethostbyname(MyName)))
return NULL;
memset((void *)&amp;in,sizeof(in),0);
in.s_addr=*((unsigned long *)thisHost->h_addr_list[0]);
if(!(ptr = inet_ntoa(in)))
return NULL;
IP = new char[strlen(ptr)+1],
strcpy(IP,ptr);
return IP;
 
多谢各位,IP控件我已经装上了,与我的程序相冲突的地方也解决了!
 
多人接受答案了。
 
后退
顶部