怎样设置IP地址的输入框,设置成象TCP/IP属性那样的IP输入框。 (50分)

  • 主题发起人 主题发起人 vpp
  • 开始时间 开始时间
V

vpp

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样设置IP地址的输入框,设置成象TCP/IP属性那样的IP输入框。
如IP显示分成4段,让每一段都居中显示。

我想知道具体的设置方法,如MaskEdit的属性设置。
 
这个应该容易吧!如果用MaskEdit应该还容易一些!
 
很多第三方控件都有此功能的.
如果不想用的话就自己编个简单的控件吧!
 
我想知道具体的设置方法,如MaskEdit的属性设置。
 
可以自己写一个组件啊,系统支持的,设置一下Edit的样式就行了

type
TIPEdit = class (TEdit)
protected
procedure CreateParams(var Params: TCreateParams); override;
end;

uses
ComCtrls, CommCtrl;

procedure TIPEdit.CreateParams(var Params: TCreateParams);
begin
inherited;
if not (csDesigning in ComponentState) then
begin
InitCommonControl(ICC_INTERNET_CLASSES);
CreateSubClass(Params, WC_IPADDRESS);
end;
end;
 
非要自己写吗?没有现成控件能设置吗?
 
我写了一个TRIPedit,如有需要请跟我联系。zysoft@sixin.com。
 
我封装了Windows里自带的IP输入控件,
本来想写篇稿子赚点儿钱,看在大家都是Delphi程序员的份上,算了,先帖出来,
不过没有注释,呵呵

unit IPEdit;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, ComCtrls, Commctrl, Forms;

type
TIPEdit = class(TWinControl)
private
{ Private declarations }
FReadOnly: Boolean;
FIpAddress: integer;
FIpAddr: string;
FAutoSize:Boolean;
FBorderStyle: TBorderStyle;
// FModified:Boolean;
FOnEnter: TNotifyEvent;
FOnChange: TNotifyEvent;
procedure WMContextMenu(var Message: TWMContextMenu);
message WM_CONTEXTMENU;
procedure WMNotify(var Message: TWMNotify);
message WM_NOTIFY;
procedure CMEnter(var Message: TCMGotFocus);
message CM_ENTER;
procedure CMFontChanged(var Message: TMessage);
message CM_FONTCHANGED;
procedure AdjustHeight;
procedure UpdateHeight;
protected
{ Protected declarations }
procedure Change; dynamic;
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateWindowHandle(const Params: TCreateParams); override;
function GetIpAddr:string;
procedure SetReadOnly(Value: Boolean);
procedure SetIpAddr(Value: string);
procedure SetBorderStyle(Value:TBorderStyle);
procedure SetAutoSize(Value:Boolean);override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure Clear;
procedure SetEditField(const IPField:integer);
published
{ Published declarations }
property ParentColor default False;
// property Modified: Boolean read FModified write SetModified;
property ReadOnly: Boolean read FReadOnly write SetReadOnly default False;
property IP:string read GetIpAddr write SetIpAddr;
property IPAddress:integer read FIpAddress;
property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsSingle;

property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
property TabStop default True;

property Anchors;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ImeMode;
property ImeName;
property ParentBiDiMode;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property Visible;

property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TIPEdit]);
end;

{ TIPEdit }

procedure TIPEdit.AdjustHeight;
var
DC: HDC;
SaveFont: HFont;
I: Integer;
SysMetrics, Metrics: TTextMetric;
begin
DC := GetDC(0);
GetTextMetrics(DC, SysMetrics);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
if NewStyleControls then
begin
if Ctl3D then I := 8 else I := 6;
I := GetSystemMetrics(SM_CYBORDER) * I;
end else
begin
I := SysMetrics.tmHeight;
if I > Metrics.tmHeight then I := Metrics.tmHeight;
I := I div 4 + GetSystemMetrics(SM_CYBORDER) * 4;
end;
Height := Metrics.tmHeight + I;
end;

procedure TIPEdit.Change;
begin
inherited;
if Assigned(FOnChange) then FOnChange(Self);
end;

procedure TIPEdit.Clear;
begin
Perform(IPM_CLEARADDRESS,0,0);
end;

procedure TIPEdit.CMEnter(var Message: TCMGotFocus);
begin
Perform(IPM_SETFOCUS,0,0);
if Assigned(FOnEnter) then FOnEnter(Self);
end;

procedure TIPEdit.CMFontChanged(var Message: TMessage);
begin
inherited;
if (csFixedHeight in ControlStyle) and not ((csDesigning in
ComponentState) and (csLoading in ComponentState)) then AdjustHeight;
end;

constructor TIPEdit.Create(AOwner: TComponent);
const
EditStyle = [csClickEvents, csSetCaption, csDoubleClicks, csFixedHeight];
begin
inherited Create(AOwner);
if NewStyleControls then
ControlStyle := EditStyle
else
ControlStyle := EditStyle + [csFramed];
Width := 120;
Height := 20;
TabStop := True;
ParentColor := False;
FBorderStyle := bsSingle;
FReadOnly := False;
FIpAddr := '';
FIpAddress := 0;
FAutoSize := False;
FOnChange := nil;
FOnEnter := nil;
end;

procedure TIPEdit.CreateParams(var Params: TCreateParams);
begin
InitCommonControl(ICC_INTERNET_CLASSES);
inherited CreateParams(Params);
CreateSubClass(Params, 'SysIPAddress32');
with Params do
begin
if NewStyleControls and Ctl3D and (FBorderStyle = bsSingle) then
begin
Style := Style and not WS_BORDER;
ExStyle := ExStyle or WS_EX_CLIENTEDGE;
end;
end;
end;

procedure TIPEdit.CreateWindowHandle(const Params: TCreateParams);
var
P: TCreateParams;
begin
if SysLocale.FarEast and (Win32Platform <> VER_PLATFORM_WIN32_NT) and
((Params.Style and ES_READONLY) <> 0) then
begin
// Work around Far East Win95 API/IME bug.
P := Params;
P.Style := P.Style and (not ES_READONLY);
inherited CreateWindowHandle(P);
if WindowHandle <> 0 then
SendMessage(WindowHandle, EM_SETREADONLY, Ord(True), 0);
end
else
inherited CreateWindowHandle(Params);
end;

function TIPEdit.GetIpAddr: string;
begin
Perform(IPM_GETADDRESS,0,Longint(@FIpAddress));
if FIpAddress<>0 then
FIpAddr:=IntToStr(FIRST_IPADDRESS(FIpAddress))+'.'+IntToStr(SECOND_IPADDRESS(FIpAddress))+'.'+IntToStr(THIRD_IPADDRESS(FIpAddress))+'.'+IntToStr(FOURTH_IPADDRESS(FIpAddress))
else
FIpAddr:='';
Result:=FIpAddr;
end;

procedure TIPEdit.SetAutoSize(Value: Boolean);
begin
if FAutoSize <> Value then
begin
FAutoSize := Value;
UpdateHeight;
end;
end;

procedure TIPEdit.SetBorderStyle(Value: TBorderStyle);
begin
if FBorderStyle <> Value then
begin
FBorderStyle := Value;
UpdateHeight;
RecreateWnd;
end;
end;

procedure TIPEdit.SetEditField(const IPField: integer);
begin
if (IPField < 4) and (IPField >0) then
begin
Perform(IPM_SETFOCUS,IPField,0);
end;
end;

procedure TIPEdit.SetIpAddr(Value: string);
var
Parase:TStringList;
begin
Parase:=TStringList.Create;
try
Parase.Delimiter:='.';
Parase.DelimitedText:=Value;
if Parase.Count = 4 then
begin
FIpAddress:= MAKEIPADDRESS(StrToInt(Parase[0]),StrToInt(Parase[1]),StrToInt(Parase[2]),StrToInt(Parase[3]));
FIpAddr:=Value;
Perform(IPM_SETADDRESS,0,FIpAddress);
end;
finally
Parase.Free;
end;
end;

procedure TIPEdit.SetReadOnly(Value: Boolean);
begin
if FReadOnly <> Value then
begin
FReadOnly := Value;
if HandleAllocated then
SendMessage(Handle, EM_SETREADONLY, Ord(Value), 0);
end;
end;

procedure TIPEdit.UpdateHeight;
begin
if FAutoSize and (FBorderStyle = bsSingle) then
begin
ControlStyle := ControlStyle + [csFixedHeight];
AdjustHeight;
end else
ControlStyle := ControlStyle - [csFixedHeight];
end;

procedure TIPEdit.WMContextMenu(var Message: TWMContextMenu);
begin
SetFocus;
inherited;
end;

procedure TIPEdit.WMNotify(var Message: TWMNotify);
begin
with Message do
begin
if IDCtrl = IPN_FIELDCHANGED then
begin
Change;
Result:=0;
end
else
inherited;
end;
end;

end.
 
楼上 是参考吗?
这个东西早就有了!
VC自己就带一个 哈哈!
 
很简单的,你可以从edit继承,写一个控件,重载一下CreateParams(),几行代码就可以搞定,
甚至不用做控件也行:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, CommCtrl;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }

public
{ Public declarations }
end;

TIpEdit = class(TEdit)
procedure CreateParams(var Params: TCreateParams); override;
//private
procedure Change; override;
end;

{ TIpEdit }

var
Form1: TForm1;
ed: TIpEdit;

implementation

{$R *.DFM}

procedure TIpEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
InitCommonControl(ICC_INTERNET_CLASSES);
CreateSubClass(Params, WC_IPADDRESS);
end;

procedure TIpEdit.Change;
begin
Form1.caption := ed.Text;
inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ed := TIpEdit.Create(self);
ed.Parent := self;
ed.Top := 10;
ed.Left := 10;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
ed.Text := '127.12.123.12';
caption := ed.Text;
end;

end.
 
我刚学delphi,真的没有现成的吗?
自己还不会写控件觉得好可怕,那是高手做的事了。
 
有呀。你可以采用MaskEdit呀。
设置如下:
maskedit1.EditMask:='000/.000/.000/.000';
你也可以在Maskedit1的EditMask属性中选择。
 
不行的,不好看,要是输入192.10.1.1,还有几条横线在。
我想设置成TCP/IP属性那样的IP输入框。
 
咳咳,你把俺的代码复制下来,保存为一个名叫IPEdit.Pas的文件,
然后将它安装到控件面板上去就可以用了

回复:itren
我只不过把微软的公用控件封装一下而已,不明白你的话
 

Similar threads

D
回复
0
查看
825
DelphiTeacher的专栏
D
D
回复
0
查看
831
DelphiTeacher的专栏
D
D
回复
0
查看
664
DelphiTeacher的专栏
D
D
回复
0
查看
824
DelphiTeacher的专栏
D
后退
顶部