Use the IP control provided by Windows

Source: Internet
Author: User

In network programs, users often need to enter IP addresses. However, C ++ Builder does not provide us with controls that can be used to input an IP string, so we have to use the TEdit control (single-line text box) to accept the IP string entered by the user. However, using TEdit to input IP strings is not a good idea, because it is very inconvenient to process. In fact, there is a Windows Control dedicated to inputting IP strings beside us, just like the control for inputting IP addresses in the Internet Protocol (TCP/IP) attribute in the network link attribute. The IP Control rejects invalid IP strings (0 ~ It allows you to easily obtain the IP value (32-bit integer) corresponding to the IP string in the control ), this saves the trouble of converting IP strings and IP values. In addition, you can restrict the IP ranges that can be entered in the IP control. In this article, I will introduce how to use the Windows IP Control in our C ++ Builder program.

Windows has two very important dynamic connection libraries: commctrl. dll and comctl32.dll, which are Windows custom control libraries (Windows Common Controls ). The custom control library contains many common Windows controls, such as Statusbar, Coolbar, and HotKey. In C ++ Builder, most of these controls have been packaged as visual controls. After Microsoft launched Internet Explorer 3, some controls were added to the custom control library, including Windows IP Address edit control ).

● Initialize the Windows custom control library ●

Windows provides two API functions: InitCommonControls and InitCommonControlsEx, which are used to initialize the custom control library. From the name, we can easily see the relationship between the two API functions: the latter is the enhancement of the former. If you want to use the IP Control in the program, you must use InitCommonControlsEx to initialize the custom control library and class. The prototype of the InitCommonControlsEx function 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 );
The IP control belongs to the ICC_INTERNET_CLASSES class. To use this control, you should include the following initialization code in the program:
TInitCommonControlsEx ICC;
ICC. dwSize = sizeof (TInitCommonControlsEx );
ICC. dwICC = ICC_INTERNET_CLASSES;
If (! InitCommonControlsEx (& ICC ))
Return; // component initialization failed

● Create an IP Control ●

Both the Windows API functions CreateWindow and createdomainwex can be used to create an IP Control instance. The window class of the IP control is "SysIPAddress32". The commctrl. pas unit of C ++ Builder defines a symbolic constant WC_IPADDRESS for it. The following statement creates an IP Control on Form1.

HWND hIpEdit = CreateWindow (WC_IPADDRESS, NULL, WS_CHILD | WS_VISIBLE, 10, 10, 135,47, Handle, 0, HInstance, NULL );

● Use IP Control ●

In the program, we send a message to the IP Control to communicate with it. The IP control can respond to the following six messages:

Message constant message value parameters and return values

IPM_CLEARADDRESSWM_USER + 100 clear IP string parameters in the IP Control

IPM_SETADDRESSWM_USER + 101 set the IP address string Lparam of the IP control to a 32-bit IP address value

IPM_GETADDRESSWM_USER + 102 obtain the IP value (32-bit Integer) Lparam corresponding to the IP string in the IP control as a pointer to the Integer variable. The returned value is equal to the number of non-controlled fields in the IP control. The obtained IP value is stored in the Integer variable pointed to by lparam.

IPM_SETRANGEWM_USER + 103 set one of the four parts of the IP Control's IP value range Wparam specifies the part of the value range to be set; lparam's low 16-bit characters are the range of this field: high byte is the upper limit, and low byte is the lower limit.

IPM_SETFOCUSWM_USER + 104 set the input focus Wparam to specify which part gets the focus

Whether the IPM_ISBLANKWM_USER + 105IP string is null or not. Return Value: If it is null, non-0 is returned. If it is not null, 0 is returned.

★(I) Clear the IP string (IPM_CLEARADDRESS)★

SendMessage (hIpEdit, IPM_CLEARADDRESS, 0, 0 );

★(Ii) set the IP string (IPM_SETADDRESS)★

Int nIP;
NIP = MAKEIPADDRESS (192,168 );
SendMessage (hIpEdit, IPM_SETADDRESS, 0, nIP );
In this example, the content of the IP control is set to "192.168.0.1", where MAKEIPADDRESS is a Win32 macro, defined in the commctrl. h header file, 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 ))))
★(3) obtain the IP value (IPM_GETADDRESS)★
Int nIP;
SendMessage (hIpEdit, IPM_GETADDRESS, 0, int (& nIP ));
// NIP ++;
// SendMessage (hIpEdit, IPM_SETADDRESS, 0, nIP); // Add 1 to the IP address and then assign it to the IP control.

To obtain the IP value corresponding to the IP string in the IP control, you should send the IPM_GETADDRESS message to the IP control and use a 32-bit integer address as the last parameter of SendMessage.

★(Iv) set the value range (IPM_SETRANGE)★

SendMessage (hIpEdit, IPM_SETRANGE, 0,200 <8 | 100 );

This statement limits the range of the first part of the IP Control to 100 ~ 200. In the IPM_SETRANGE message, Wparam indicates the field to be set, and the low 16-bit character of lparam indicates the range of this field: the high byte is the upper limit, and the low byte is the lower limit.

★(V) set the input focus (IPM_SETFOCUS)★

SendMessage (hIpEdit, IPM_SETFOCUS, 3, 0 );

// Set the input focus to the fourth part of the IP control.

(6) Determine whether the IP string is null (IPM_ISBLANK)

If (! SendMessage (hIpEdit, IPM_ISBLANK, 0, 0 ))
{
// The IP string in the IP control is null.
}
Else
{
// At least some of the IP strings in the IP control are not empty
}

● IP Control notification message ●

When the IP string is changed or the input focus is transferred, 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 processing the notification message IPN_FIELDCHANGED:

Void _ fastcall TForm1: WndProc (TMessage & Msg)
{
LPNMHDR p = (LPNMHDR) Msg. LParam;
If (Msg. Msg = wm_policy)
{
If (p-> code = IPN_FIELDCHANGED)
{
// Process the IPN_FIELDCHANGED notification message of the IP Control
}
}
TForm: WndProc (Msg );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.