【问题标题】:Seeking FOSS IPv4 address picker VCL component求FOSS IPv4地址选择器VCL组件
【发布时间】:2012-03-09 03:04:05
【问题描述】:

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

如果我还可以输入“localhost”之类的内容,还有额外的奖励,尽管我可以不用它。

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

当然,我可以通过一些蒙版编辑来敲出一个,但肯定有人以前发明过那个特殊的 (FOSS) 轮子吗?


自我提醒:如果您必须编写代码,this page 看起来很有用

【问题讨论】:

    标签: delphi vcl


    【解决方案1】:

    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 和答案,因为它是独立的(不确定其他建议是否需要我安装所有 JVCL)。谢谢
    【解决方案2】:

    试试JEDI Visual Component Library 中的TJvIPAddress 怎么样?它具有自动范围值校正,源自标准的 Windows IP 编辑框。

    【讨论】:

    • 不客气。它实际上与雷米在此处发布的相同;在 Windows IP 地址控件上,但它只是作为组件包装。
    猜你喜欢
    • 2021-06-29
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2016-10-17
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多