这是一个实例,然后根据你自己的需要调整适合自己业务需要的部份。
unit DatePick;
interface
uses windows,messages,classes,forms,stdctrls,buttons,controls,sysutils;
Type TDatePick = class(TWinControl)
private
FEdit : TEdit;
Fspbt : TSpeedButton;
//FBHeight:integer;
procedure FSetSpbtHeight(Value:integer);
function FGetSpbtHeight:integer;
procedure FSetSpbtTop(Value:integer);
function FGetSpbtTop:integer;
procedure FSetEditText(Value:string);
function FGetEditText:string;
procedure FSetReadonly(Value:boolean);
function FGetReadonly:boolean;
procedure FOnSpbtClick(Sender:TObject);
procedure FSetChange(Value:TNotifyEvent);
function FGetChange:TNotifyEvent;
procedure WMSize(var msg:TWMSize); message WM_Size;
public
constructor create(aOwner:Tcomponent);override;
destructor Destroy;override;
published
property ctl3d;
property BHeight:integer read FGetSpbtHeight write FSetSpbtHeight;
property BTop :integer read FGetSpbtTop write FSetSpbtTop;
property Text : string read FGetEditText Write FSetEditText;
property ONDateChange:TNotifyEvent read FGetChange write FSetChange;
property TextReadOnly:boolean read FGetReadonly write FSetReadonly default true;
property TabOrder;
//property TEdit.Fonts;
end;
procedure Register;
implementation
uses SelDateR;
{$R DATEPICK.RES}
constructor TDatePick.create(aOwner:Tcomponent);
begin
inherited Create(aOwner);
FEdit := TEdit.Create(self);
FEdit.Width := 75;
FEdit.Height := 19;
FEdit.Text := '';//DateTostr(now);
FEdit.Parent := self;//TWinControl(aOwner);
FEdit.Visible := true;
FEdit.Ctl3D := false;
FEdit.ReadOnly := true;
//FEdit.Top := self.Top;
//FEdit.Left := self.Left;
self.Height := self.FEdit.Height +2;
Fspbt := TSpeedButton.Create(self);
Fspbt.Width := 22;
Fspbt.Height := 19;
Fspbt.Left := self.FEdit.Left + self.FEdit.Width+1;
Fspbt.Top := self.FEdit.Top;
Fspbt.Parent := self;//TWinControl(aOwner);
Fspbt.Visible := true;
Fspbt.Glyph.LoadFromResourceName(HInstance,'SPBTBMP');
Fspbt.OnClick := self.FOnSpbtClick;
width := self.FEdit.Width+self.Fspbt.Width+1;
height := self.FEdit.Height;
// Fspbt.NumGlyphs := 1;
end;
destructor TDatePick.Destroy;
begin
FEdit.Parent := nil;
FEdit.Free;
Fspbt.Parent := nil;
Fspbt.Free;
inherited;
end;
procedure TDatePick.FOnSpbtClick(sender:Tobject);
begin
with TPickJSDate.Create(nil) do
try
if self.FEdit.Text <> '' then
sDt := self.FEdit.Text
else
sDt := DateTostr(now);
if showmodal = mrok then
begin
self.FEdit.Text := sDt
end;
self.FEdit.SetFocus;
finally
free;
end;
end;
procedure TDatePick.FSetChange(Value:TNotifyEvent);
begin
self.FEdit.OnChange := Value;
end;
{ 设置 spbt 的高度 }
procedure TDatePick.FSetSpbtHeight(Value:integer);
begin
self.Fspbt.Height := Value;
end;
function TDatePick.FGetSpbtHeight;
begin
result := self.Fspbt.Height;
end;
function TDatePick.FGetChange:TNotifyEvent;
begin
result := self.FEdit.OnChange;
end;
procedure TDatePick.WMSize(var msg:TWMSize);
begin
inherited;
self.FEdit.Width := msg.Width-self.Fspbt.Width;
self.Fspbt.Left := self.FEdit.Width;
self.FEdit.Height := msg.Height;
// self.Fspbt.Width := msg.Width;
self.Fspbt.Height := msg.Height;
end;
{ 设置 spbt 的 top }
procedure TDatePick.FSetSpbtTop(Value:integer);
begin
self.Fspbt.Top := value;
end;
function TDatePick.FGetSpbtTop:integer;
begin
result := self.Fspbt.Top;
end;
{ 设置 Edit 的 Text }
procedure TDatePick.FSetEditText(Value:string);
begin
{try
StrToDate(Value)
except
messagebox(self.Handle,pchar('无效的日期'),pchar('提示'),mb_ok+mb_applmodal);
exit;
end;
}
self.FEdit.Text := Value;
end;
function TDatePick.FGetEditText:string;
begin
result := self.FEdit.Text;
end;
{ 设置 TextRealonly 属性 }
procedure TDatePick.FSetReadonly(value:boolean);
begin
self.FEdit.ReadOnly := value;
end;
function TDatePick.FGetReadonly:boolean;
begin
result := self.FEdit.ReadOnly;
end;
procedure Register;
begin
RegisterComponents('System',[TDatePick]);
end;
end.