IP地址输入框制作

该博客介绍了一个自定义的IP地址输入框组件的实现,包括获取和设置IP地址的功能,以及处理非法IP地址的验证。通过Delphi的TForm和相关控件,实现了窗口消息的处理,如IPM_SETADDRESS和IPM_GETADDRESS,确保输入的IP地址有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

unit IpInput;

interface

uses
  Windows,Dialogs,SysUtils,Classes,Graphics,Forms,Controls,StdCtrls,Buttons,ExtCtrls,
            ComCtrls,Commctrl;

type
  TInputIpFrm = class(TForm)
    SpdBfdftnCancle: TLabel;
    SpdBtnOK: TSpeedButton;
    SpdBtnCancle: TSpeedButton;
    procedure FormShow(Sender: TObject);
    procedure SpdBtnOKClick(Sender: TObject);
    procedure SpdBtnCancleClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private

  public
    ipEdit:HWND;
    m_CommunicationIP:String;
    ipAddress:integer;
   
    procedure GetIPNum(strIniIP:String;out nFirst,nSecond,nThird,nForth:integer);
  end;

var
  InputIpFrm: TInputIpFrm;


implementation


{$R *.dfm}


procedure TInputIpFrm.GetIPNum(strIniIP:String;out nFirst,nSecond,nThird,nForth:integer);
var
    str,strNum,strTemp:String;
begin
    str:='.';
    try
        strTemp:=strPos(PChar(strIniIP),PChar(str));
        strNum:=Copy(strIniIP,1,Length(strIniIP) - Length(strTemp));
        nFirst:=StrToInt(strNum); 

        strIniIP:=Copy(strTemp,2, Length(strTemp));
        strTemp:=strPos(PChar(strIniIP),PChar(str));
        strNum:=Copy(strIniIP,1,Length(strIniIP) - Length(strTemp));
        nSecond:=StrToInt(strNum);

        strIniIP:=Copy(strTemp,2, Length(strTemp));
        strTemp:=strPos(PChar(strIniIP),PChar(str));
        strNum:=Copy(strIniIP,1,Length(strIniIP) - Length(strTemp));
        nThird:=StrToInt(strNum);

        strIniIP:=Copy(strTemp,2, Length(strTemp));
        strTemp:=strPos(PChar(strIniIP),PChar(str));
        strNum:=Copy(strIniIP,1,Length(strIniIP) - Length(strTemp));
        nForth:=StrToInt(strNum);
    except
        nFirst:=192;
        nSecond:=168;
        nThird:=0;
        nForth:=198;
        showmessage('');
    end;

end;


procedure TInputIpFrm.FormShow(Sender: TObject);
var
    nFirst,nSecond,nThird,nForth:integer;
begin
    GetIPNum(m_CommunicationIP, nFirst, nSecond, nThird, nForth);

    ipAddress:=MAKEIPADDRESS(nFirst,nSecond,nThird,nForth);//初始化 IP地址

    SendMessage(ipEdit,IPM_SETADDRESS,0,ipAddress);//show   the   current   address
   
    SendMessage(ipEdit,IPM_SETFOCUS,0,0); //select   the   first   field
     { }
end;

procedure TInputIpFrm.SpdBtnOKClick(Sender: TObject);
begin
    SendMessage(ipEdit,IPM_GETADDRESS,0,longint(@ipAddress));

    if ipAddress = 0   then
    begin
        beep;
        SendMessage(ipEdit,IPM_SETFOCUS,0,0);//select   the   first   field
        Application.MessageBox('IP地址非法!','警告',mb_OK + MB_ICONWARNING);
        modalResult:=mrNone;
    end
    else
    begin       
        m_CommunicationIP:=IntToStr(FIRST_IPADDRESS(ipAddress))+'.'
                          +IntToStr(SECOND_IPADDRESS(ipAddress))+'.'
                          +IntToStr(THIRD_IPADDRESS(ipAddress))+'.'
                          +IntToStr(FOURTH_IPADDRESS(ipAddress));
                      { }
        self.Close;
    end;

end;

procedure TInputIpFrm.SpdBtnCancleClick(Sender: TObject);
begin
    self.Close;
end;

procedure TInputIpFrm.FormDestroy(Sender: TObject);
begin
    InputIpFrm:=nil;
end;

procedure TInputIpFrm.FormCreate(Sender: TObject);
begin
    InitCommonControl(ICC_INTERNET_CLASSES);

    ipEdit:=CreateWindow(WC_IPADDRESS,nil,WS_CHILD   or   WS_VISIBLE,
                        65,20,160,25,self.Handle,0,hInstance,nil);

end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值