----- In network programs, we often encounter situations where users are required to enter IP addresses. However, Delphi does not provide us with a control 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 entered by the user. However, using Tedit to enter the IP string is not a good idea because it is very inconvenient to handle. In fact, there is a Windows control beside us specifically for entering IP strings, as shown in the figure. The IP control will reject illegal IP strings (only numbers between 0..255 can be entered in each part); it allows you to easily obtain the IP value (32-bit integer) corresponding to the IP string in the control. This This saves you the trouble of converting between IP strings and IP values; in addition, you can also limit the range of IPs that can be entered in the IP control. In this article, I will introduce to you how to use Windows IP controls in our Delphi programs.
---- There are two very important dynamic link libraries in Windows: commctrl.dll and comctl32.dll, which are Windows' custom control libraries (Windows Common Controls). The custom control library contains many commonly used Windows controls, such as Statusbar, Coolbar, HotKey, etc.; in Delphi, most of these controls have been packaged as visual controls. After Microsoft launched Internet Explorer 3, some new controls were added to the custom control library, including the Windows IP control (IP Address edit control).
---- Initialize Windows custom control library
---- Windows provides two API functions, InitCommonControls and InitCommonControlsEx, for initializing custom control libraries. From the names, it is not difficult to see the relationship between these two API functions: the latter is an enhancement of the former. If you want to use IP controls in your program, you must use InitCommonControlsEx to complete the initialization of custom control libraries and classes. The prototype of the function InitCommonControlsEx is as follows (Pascal syntax):
----...
---- Create IP control
----...
---- Use IP control. In the program, we communicate with the IP control by sending messages to it. The IP control can respond to the following six messages. These messages and their meanings are shown in the table below:
----...
---- If you want to get the IP value corresponding to the IP string in the IP control, you should send the IPM_GETADDRESS message to the IP control, and you need to use a 32-bit integer address as the last parameter of SendMessage.
----...
---- Notification message of IP control
---- When the IP string is changed or the input focus is transferred, the IP control will send the notification message IPN_FIELDCHANGED to its parent window. In most cases, we can ignore this notification message. The following is an example of handling the notification message IPN_FIELDCHANGED:
PRocedure Tform1.WndProc(var Msg: TMessage);var p:PNMHDR;begininherited; if Msg.Msg=WM_NOTIFYthen begin p:=Pointer(Msg.lParam); if p^.code=IPN_FIELDCHANGED then begin{...processing of IP controls IPN_FIELDCHANGED notification message...} end;end;end;