下面是我做的一个控件,你看一下吧
里边有ONEXIT,我想ONENTER也一样
unit ArmLabEdit;
interface
uses
Windows, Messages, stdctrls,SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TEditStyle=(EsNumber,EsText);
TArmLabEdit = class(TWinControl)
private
FEdit:TEdit;
FLab:TLabel;
FLength:integer;
FEditStyle:TEditStyle;
FExit:TNotifyEvent;
FKey:TKeyPressEvent;
function GetCaption: string;
function GetText: string;
//设焦点
procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
//设计退出事件
procedure CMFocusChanged(var Message: TCMFocusChanged); message CM_FOCUSCHANGED;
//设计击键事件
procedure CMChildKey(var Message: TMessage); message CM_CHILDKEY;
procedure SetText(const Value: string);
procedure SetCaption(const Value: string);
procedure WMSize(var message:tmessage) ;message wm_size;
procedure cmfontchanged(var message :Tmessage);message CM_fontchanged;
procedure SetEditStyle(const Value: TEditStyle);
protected
public
constructor Create(aowner:tcomponent);override;
destructor Destory;
published
property Text:string read gettext write settext;
property Caption :string read getcaption write setcaption;
property Style:TEditStyle read FEditStyle write SetEditStyle;
property TxtLength:integer read Flength write Flength;
property OnExit read FExit write FExit;
property Onkeypress read Fkey write Fkey;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('宫雨', [TArmLabEdit]);
end;
{ TArmLabEdit }
procedure TArmLabEdit.CMChildKey(var Message: TMessage);
begin
if Assigned(FKey) then
FEdit.OnKeyPress:=Fkey;
end;
procedure TArmLabEdit.CMFocusChanged(var Message: TCMFocusChanged);
begin
if Assigned(FExit) then
FEdit.OnExit:=FExit;
end;
procedure TArmLabEdit.cmfontchanged(var message: Tmessage);
begin
inherited;
end;
constructor TArmLabEdit.Create(aowner: tcomponent);
begin
inherited Create(aowner);
Width:=200;
Height:=25;
FLab:=tlabel.Create(self);
FLab.Font.Name:='宋体';
FLab.Font.Size:=12;
FLab.Parent:=self;
FLab.Left:=left;
FLab.Width:=75;
FLab.height:=25;
FLab.Visible:=true;
FEdit:=tedit.Create(self);
FEdit.Parent:=self;
FEdit.Left:=left+flab.Width+5;
FEdit.Width:=width-5-flab.Width;
FEdit.height:=25;
FEdit.Font.Name:='宋体';
FEdit.Font.Size:=10;
end;
destructor TArmLabEdit.Destory;
begin
FLab.free;
FEdit.Free;
inherited;
end;
function TArmLabEdit.GetCaption: string;
begin
Result:=Flab.Caption;
end;
function TArmLabEdit.gettext: string;
begin
result:=FEdit.Text;
end;
procedure TArmLabEdit.SetCaption(const Value: string);
begin
FLab.Caption:=Value;
end;
procedure TArmLabEdit.SetEditStyle(const Value: TEditStyle);
begin
FEditStyle := Value;
end;
procedure TArmLabEdit.SetText(const Value: string);
begin
if length(value)<=Flength then
FEdit.Text:=value
else
FEdit.Text:=copy(value,1,flength);
end;
procedure TArmLabEdit.WMSetFocus(var Message: TWMSetFocus);
begin
inherited;
FEdit.SetFocus;
end;
procedure TArmLabEdit.wmsize(var message: tmessage);
begin
inherited;
FLab.height:=Height;
FEdit.Width:=Height;
FEdit.Width:=Width-Flab.Width-5;
end;
end.