Use the IP control provided by Windows

Source: Internet
Author: User

In the networkProgram. 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 items in the program:Code:
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. The following table lists these messages and their meanings:

Message constant message value parameters and return values

Ipm_clearaddress wm_user+ 100

Clear IP string parameters in the IP Control

 

Ipm_setaddress wm_user+ 101

Set the IP address string lparam of the IP control to a 32-bit IP address.

 

Ipm_getaddress wm_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_setrange: wm_user + 103

Set the IP value range of one of the four parts of the IP Control wparam to specify the part of the value range; lparam's low 16-bit character is the range of this field: the high byte is the upper limit, the lower byte is the lower limit.

 

Ipm_setfocus wm_user + 104

Set the input focus wparam to specify which part gets the focus

 

Ipm_isblank wm_user+ 105

Whether the IP address 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 );
}

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.