Use the built-in IP address edit box in C ++ Builder.
1. In the BCB project, use the IP address box in the form.
First reference the header file, and then declare the variable.
[Cpp]
# Include "SysIPAddress. hpp"
TSysIPAddress32 * ipaddr;
2. Create the control in the FormCreate event and specify the location:
[Cpp]
Ipaddr = new TSysIPAddress32 (this );
Ipaddr-> Parent = this;
Ipaddr-> Left = 5;
Ipaddr-> Top = 5;
3. Use Controls:
[Cpp]
Int nIP;
AnsiString ss;
Ipaddr-> Enabled = false;
If (ipaddr-> IsEmpty) ShowMessage ("is the IP address empty? ");
// Obtain the IP address
Edit1-> Text = ipaddr-> Text;
NIP = ipaddr-> IPAddr;
Ss. sprintf ("| 0x % X", nIP );
Edit1-> Text = Edit1-> Text + ss;
Ipaddr-> ClearIP ();
NIP = MAKEIPADDRESS (192,168 );
Ipaddr-> IPAddr = nIP;
Ipaddr-> Enabled = true;
4. After use, release the control in the FormClose event:
[Cpp]
Delete ipaddr;
5. TSysIPAddress32 encapsulation code:
[Cpp]
//-------------------------------------------------------------
// SysIPAddress. hpp
//-------------------------------------------------------------
# Ifndef TSYSIPADDRESS32H
# Define TSYSIPADDRESS32H
# Include <vcl. h>
# Include <windows. h>
Class TSysIPAddress32: public TWinControl
{
Public:
_ Property Text;
_ Property Font;
_ Property Enabled;
_ Property TabStop;
_ Property Handle;
// _ Property Color = {default = 0x292929 };
_ Property bool IsEmpty = {read = _ isEmpty };
_ Property int IPAddr = {read = _ getIP, write = _ setIP };
Void ClearIP (void ){
// Clear the IP control content IPM_CLEARADDRESS
SendMessage (Handle, IPM_CLEARADDRESS, 0, 0 );
}
_ Fastcall TSysIPAddress32 (Classes: TComponent * AOwner)
: TWinControl (AOwner)
{
Length = 150;
Height = 22;
Visible = true;
TabStop = true;
};
Protected:
Void _ fastcall CreateParams (Controls: TCreateParams & Params)
{
TWinControl: CreateParams (Params );
CreateSubClass (Params, "SysIPAddress32 ");
Params. Style | = WS_TABSTOP;
}
Private:
Bool _ isEmpty (void ){
Bool bret = false;
If (SendMessage (Handle, IPM_ISBLANK, 0, 0 ))
{
Bret = true;
}
Return bret;
}
Int _ getIP (void ){
Int nIP;
// Obtain the IP value's 32-bit integer value (IPM_GETADDRESS)
SendMessage (Handle, IPM_GETADDRESS, 0, int (& nIP ));
Return nIP;
}
Void _ setIP (int _ ip ){
// Set IPM_SETADDRESS
// NIP = MAKEIPADDRESS (192,168 );
SendMessage (Handle, IPM_SETADDRESS, 0, _ ip );
}
};
# Endif