求FOSS IPv4地址选择器VCL组件

时间:2012-03-09 03:04:06

标签: delphi vcl

严格用于保留的地址范围,因此IPv4足够好。我还不知道我是否会使用A,B或C级(可能是C,但......)所以能够处理所有这些都是一个奖励。

如果我还能输入类似“localhost”的东西,还有额外的奖励,虽然我可以没有它。

因此,最低要求是指定192.xxx.xxx.xxx并确保xxx不超过255.

当然,我可以用一些面具编辑来敲打一个,但肯定有人在之前发明了那个特殊的(FOSS)轮子?


自我注意:如果您必须对其进行编码,this page看起来很有用

2 个答案:

答案 0 :(得分:5)

Windows内置IP Address edit control。您可以将其包装在自定义TWinControl后代组件中,以便于访问和重用,例如:

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

  TIPAddress = class(TWinControl)
  private
    FOnFieldChange: TIPAddressFieldChange;
    function GetIP: String; 
    function GetIsEmpty: Boolean; 
    procedure SetIP(const Value: String);    
  protected
    procedure CreateParams(var Params: TCreateParams); override;
    procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
    procedure WMGetDlgCode(var Message: TMessage); message WM_GETDLGCODE; 
    procedure WMSetFont(var Message: TWMSetFont); message WM_SETFONT;
  public
    constructor Create(Owner: TComponent); override; 
    procedure Clear;
    property IP: String read GetIP write SetIP; 
    property IsEmpty: Boolean read GetIsEmpty; 
  published:
    property OnFieldChange: TIPAddressFieldChange read FOnFieldChange write FOnFieldChange;
  end; 

uses
  Commctrl;

constructor TIPAddress.Create(Owner: TComponent);
begin
  inherited;
  InitCommonControl(ICC_INTERNET_CLASSES}; 
end;

procedure TIPAddress.CreateParams(var Params: TCreateParams);
begin
  inherited;
  CreateSubClass(Params, WC_IPADDRESS); 
  Params.Style := WS_CHILD or WS_TABSTOP or WS_VISIBLE; 
  if NewStyleControls and Ctl3D then
  begin 
    Params.Style := Params.Style and not WS_BORDER; 
    Params.ExStyle := Params.ExStyle or WS_EX_CLIENTEDGE; 
  end;
  Params.WindowClass.style := Params.WindowClass.style and not (CS_HREDRAW or CS_VREDRAW); 
end;

procedure TIPAddress.Clear;
begin
  Perform(IPM_CLEARADDRESS, 0, 0);
end;

function TIPAddress.GetIP: String;
var
  dwIp: DWORD; 
begin
  dwIp := 0; 
  Perform(IPM_GETADDRESS, 0, LPARAM(@dwIp)); 
  Result := Format('%d.%d.%d.%d', [FIRST_IPADDRESS(dwIp), SECOND_IPADDRESS(dwIp), 
THIRD_IPADDRESS(dwIp), FOURTH_IPADDRESS(dwIp)]); 
end;

function TIPAddress.GetIsEmpty: Boolean;
begin
  Result := Perform(IPM_ISBLANK, 0, 0) <> 0; 
end;

procedure TIPAddress.SetIP(const Value: String);
var
  dwIP: LPARAM;
begin
  with TStringList.Create do try
    Delimiter := '.';
    StrictDelimiter := True;
    DelimitedText := Value;
    Assert(Count = 4);
    dwIP := MAKEIPADDRESS(StrToInt(Strings[0]), StrToInt(Strings[1]), StrToInt(Strings[2]), StrToInt(Strings[3]));
  finally
    Free;
  end;
  Perform(IPM_SETADDRESS, 0, dwIP);
end;

procedure TIPAddress.CNNotify(var Message: TWMNotify);
begin
  inherited;
  if (Message.NMHdr^.code = IPN_FIELDCHANGED) and Assigned(FOnFieldChange) then
  begin
    with PNMIPAddress(Message.NMHdr)^ do
      FOnFieldChange(Self, iField, iValue);
  end;
end;

procedure TIPAddress.WMGetDlgCode(var Message: TMessage);
begin
  inherited; 
  Message.Result := Message.Result or DLGC_WANTARROWS; 
end;

procedure TIPAddress.WMSetFont(var Message: TWMSetFont); 
var 
  LF: LOGFONT; 
begin 
  if GetObject(Message.Font, SizeOf(LF), @LF) <> 0 then
  begin 
    Message.Font := CreateFontIndirect(LF); 
    inherited; 
  end; 
end; 

答案 1 :(得分:3)

如何尝试TJvIPAddress中的JEDI Visual Component Library?它具有自动范围值校正功能,它来自标准的Windows IP编辑框。