2012-03-09 67 views
2

嚴格用於保留地址範圍,所以IPv4足夠好。我不知道我是否會使用A,B或C類(可能是C,但是......),所以它能夠處理所有的問題是一種獎勵。尋求FOSS IPv4地址選擇器VCL組件

如果我還可以輸入諸如「localhost」之類的東西,還可以獲得額外的獎勵,儘管我可以沒有這種生活。

因此,最低要求是指定一個192.xxx.xxx.xxx,並確保XXX不超過255

當然,我可以敲一起來的幾個面具編輯,但肯定有人發明了特殊的(福斯)車輪?


自我提醒:如果你必須編寫它,this page看起來有用

回答

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; 
+0

+1和答案,因爲它是獨立的(不知道其他建議是否需要我安裝所有的JVCL)。謝謝 – Mawg 2012-03-13 08:06:30

3

什麼嘗試從JEDI Visual Component LibraryTJvIPAddress?它具有自動範圍值校正功能,它來自標準Windows IP編輯框。

+0

+1看起來不錯,謝謝 – Mawg 2012-03-13 08:05:15

+0

不客氣。它實際上是基於雷米在這裏發佈的相同;在Windows IP地址控制上,但它只是包裝爲組件。 – TLama 2012-03-13 08:07:03