This article is Ccrun (old demon) based on an article seen by accidents. Reference:
Title: Using IP Controls in Delphi
Author: Xi'an Jiaotong University, 9649 # Liu Minghua
Thanks for some selfless dedication to the original author! Please keep the full copyright information if you want to reprint.
If this article infringes your copyright, please let us know: info@ccrun.com
Ccrun (old demon) (www.ccrun.com) 2003.10.29
In the network program, we often encounter the case where you need the user to enter the IP address. However, C Builder does not provide us with controls that can be used to enter the IP string, so we have to use the TEDIT control (single line text box) to accept the IP string input by the user. However, using TEDIT to enter an IP string is not a good idea because it is very inconvenient to process. In fact, there is a Windows control that is specifically used to enter the IP string, entered the IP control in the Internet Protocol (TCP / IP) property, in the Internet Link Properties. The IP control will refuse illegal IP strings (only numbers between 0 to 255) between each part); it allows you to easily get the IP value corresponding to the IP string in the control (32-bit integers), this province Go to the trouble between the IP string and IP values; in addition, you can limit the range of IPs that can be entered in the IP control. In this article, I will introduce you how to use Windows IP controls in our C Builder program.
There are two very important dynamic linkages in Windows: Commctrl.dll and ComctL32.dll, which are Windows Common Controls. The custom control library contains many commonly used Windows controls such as Statusbar, Coolbar, Hotkey, etc .; most of these controls have been packaged into visual controls in C Builder. After Microsoft launches Internet Explorer 3, some controls have been added to the custom control library, including the IP Address Edit Control.
● Initialize Windows Custom Control Library ●
Windows offers two API functions, INITCOMMONCONTROLS, and INITCOMMONCONTROLSEX to initialize custom control libraries. From the name, we are not difficult to see the relationship between these two API functions: the latter is the enhancement of the former. If you want to use IP controls in the program, you must use INTCOMMONCONTROLSEX to complete the initialization of custom control libraries and classes. The prototype of the function initcommonControlsex is as follows:
Typedef struct taginitcommonControlsex {
DWORD DWSIZE; // Size of this Structure
DWORD dwicc; // flags indicating which classes to be initialized
INITCOMMONCONTROLSEX, * LPINITCOMMONCONTROLSEX;
WinCommctrlapi Bool WinApi INITCOMMONCONTROLSEX (LpinitCommonControlsex);
IP controls belong to the control of ICC_INTERNET_CLASES Class, if you want to use this control, you should include the following initialization code:
TinitcommonControlsex ICC; icc.dwsize = sizeof (TinitCommonControlsex);
Icc.dwicc = ICC_INTERNET_CLASSES;
IF (! INITCOMMONCONTROLSEX (& ICC))
Return; // Initialization component failed
● Create an IP control ●
Both Windows API functions CREATEWINDOW or CREATEWINDOWEX can be used to create an IP control instance. The window class named "sysipaddress32" and C Builder's CommCtrl.PAS unit defines a symbolic WC_IPAddress. The following statement will create an IP control on Form1.
HWnd Hipedit = CreateWindow (ws_ipaddress, null, ws_child | ws_visible, 10, 10, 135, 47, handle, 0, hinstance, null);
● Use IP controls ●
In the program, we communicate with it by sending messages to the IP control. There are 6 messages that IP controls can respond, these messages and their meaning are shown below:
Message constant message value action parameters and return values
IPM_CLEARADDRESS WM_USER 100 Clear IP string parameters in the IP control
IPM_SETADDRESS WM_USER 101 Sets IP controls of IP controls for 32-bit IP values
IPM_Getaddress WM_USER 102 Gets the IP value (32-bit integer) LParam corresponding to the IP control in the IP control is a pointer to the Integer variable. The return value is equal to the number of non-controlled fields in the IP control; the acquired IP value is stored in the Integer variable pointed to by LPARAM.
IPM_STRANGE WM_USER 103 Sets one of the IP Value Range WPARAMs in 4 parts of IP Controls indicate part of the value range; LParam's low 16-bit word is the range of this field: high byte is the upper limit, low byte is Lower limit.
IPM_SETFOCUS WM_USER 104 set input focus WPARAM indicates which part to get focus
IPM_ISBLANK WM_USER 105 IP strings are empty parameters. Return value: If it is empty, return non-0; not empty, return 0
★ (1) Empty IP string (IPM_CLEARADDRESS) ★
SendMessage (Hipedit, IPM_Clearaddress, 0,0);
★ (ii) Set IP string (IPM_SetAddress) ★
int NIP;
NIP = Makeipaddress (192, 168, 0, 1);
SendMessage (Hipedit, IPM_SETADDRESS, 0, NIP);
This example sets the contents of the IP control to "192.168.0.1", where makeipaddress is a Win32 macro, defined in the command, which is used to synthesize a 32-bit IP value:
#define makeipaddress (b1, b2, b3, b4)
((LPARAM) ((DWORD) (B1) << 24) (DWORD) (B2) << 16) ((DWORD) (B3) << 8) ((DWORD) (B4))) ★ ㈢ Get IP value (IPM_Getaddress) ★
int NIP;
SendMessage (Hipedit, IPM_Getaddress, 0, INT (& NIP));
// NIP ;
// sentMessage (Hipedit, IPM_SetAddress, 0, NIP); // Assumption IP plus 1 assigns an IP control.
To get the IP value corresponding to the IP string in the IP control, you should send IPM_Getaddress messages to the IP control, and you need to put an address of a 32-bit integer as the last parameter of SendMessage.
★ ㈣㈣ Set value range (IPM_SETRANGE) ★
SendMessage (Hipedit, IPM_SetRANGE, 0, 200 << 8 | 100);
This statement limits the first portion of the IP control to 100 to 200. In the IPM_SetRange message, WPARAM indicates the field to be set, and the low 16-bit word of LPARAM is the range of the field: high byte is the upper limit, the low byte is lower.
★ ㈤ set input focus (ipm_setfocus) ★
SendMessage (Hipedit, IPM_SETFOCUS, 3, 0);
// Set the input focus in the fourth part of the IP control.
㈥ ㈥ ㈥ i i (i i)
IF (! sendit, ipm_isblank, 0,0))
{
// IP string in the IP control is empty
}
Else
{
// IP strings in the IP control are not empty
}
● Notification message for IP control ●
When the IP string is changed or the input focus occurs, the IP control sends a notification message ipn_fieldchanged to its parent window. In most cases, we can ignore this notification message. The following is an example of the processing notification message IPN_FIELDCHANGED:
Void __fastcall tform1 :: wndproc (tMessage & msg)
{
LPNMHDR P = (lpnmhdr) msg.lparam;
IF (msg.msg == wm_notify)
{
IF (p-> code == ipn_fieldchanged)
{
/ / Handle IPN_FieldChanged Notification Messages for IP Controls
}
}
TFORM :: WndProc (MSG);
}