unit IPAddrCtrl;
interface
uses
Messages, Windows, SysUtils, Classes, Controls;
type
TIPAddress = class (TCustomEdit)
private
function GetAddress: DWORD;
procedure SetAddress(InVal: DWORD);
protected
procedure CreateParams(var Params: TCreateParams); override;
public
// 入力された IP Address をクリアする
procedure Clear; override;
// IP Address が空かどうかを判断する
function IsBlank: Boolean;
// Index (0~3) で指定された位置にフォーカスを移動する
procedure SetFocusCol(Index: Integer);
// 入力可能なIPアドレスの範囲を設定する
procedure SetIPRange(LowRange, HighRange: DWORD);
published
// DWORD 表現の IP Address を読み書きする (文字列での取得は Text プロパティを使う)
property Address: DWORD read GetAddress write SetAddress;
end;
// 4組の byte から、DWORD 表現の IP Address を得る
function MakeIPAddress(b0, b1, b2, b3: byte): DWORD;
const
IPM_CLEARADDRESS = WM_USER + 100;
IPM_SETADDRESS = WM_USER + 101;
IPM_GETADDRESS = WM_USER + 102;
IPM_SETRANGE = WM_USER + 103;
IPM_SETFOCUS = WM_USER + 104;
IPM_ISBLANK = WM_USER + 105;
implementation
// http://msdn.microsoft.com/en-us/library/bb761374%28VS.85%29.aspx
function MakeIPAddress(b0, b1, b2, b3: byte): DWORD;
begin
result := (b0 shl 24) + (b1 shl 16) + (b2 shl 8) + b3;
end;
procedure TIPAddress.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
CreateSubClass(Params, 'SysIPAddress32');
end;
procedure TIPAddress.Clear;
begin
SendMessage(Self.Handle, IPM_CLEARADDRESS, 0, 0);
end;
function TIPAddress.IsBlank: Boolean;
begin
result := SendMessage(Self.Handle, IPM_ISBLANK, 0, 0) <> 0;
end;
function TIPAddress.GetAddress: DWORD;
var
IP: DWORD;
begin
result := 0;
SendMessage(Self.handle, IPM_GETADDRESS, 0, LParam(IP));
result := IP;
end;
procedure TIPAddress.SetAddress(InVal: DWORD);
begin
SendMessage(Self.handle, IPM_SETADDRESS, 0, LParam(InVal));
end;
procedure TIPAddress.SetFocusCol(Index: Integer);
begin
SendMessage(Self.handle, IPM_SETADDRESS, WParam(Index), 0);
end;
procedure TIPAddress.SetIPRange(LowRange, HighRange: DWORD);
const
dMask = $000000FF;
var
Range: WORD;
begin
// First
Range := ((LowRange shr 24) and dMask) + (((HighRange shr 24) and dMask) shl 8);
SendMessage(Self.handle, IPM_SETRANGE, 0, LParam(Range));
// Second
Range := ((LowRange shr 16) and dMask) + (((HighRange shr 16) and dMask) shl 8);
SendMessage(Self.handle, IPM_SETRANGE, 1, LParam(Range));
// Third
Range := ((LowRange shr 8) and dMask) + (((HighRange shr 8) and dMask) shl 8);
SendMessage(Self.handle, IPM_SETRANGE, 2, LParam(Range));
// Fourth
Range := ( LowRange and dMask) + (( HighRange and dMask) shl 8);
SendMessage(Self.handle, IPM_SETRANGE, 3, LParam(Range));
end;
end.
|