//现在可以设置时间格式,不过要注意,Kind属性最好与DateTimeMask相对应。
//时间仓促,文档不全,你先在D3先编译试试,如果行,我再寄个完整的Zip包给你
//使用方法与TDBEdit相同
{
作者: 沈前卫(shenqw@cmmail.com)
DelphiX的TDateTimePicker的控件非常好用,但他有个缺点
(也是他的优点),就是其时间的显示格式会随当前程序所运行
的计算机的日期与时间的格式设置不同而显示不同.有时因为
特殊原因需要使TDateTimePicker的时间与日期显示的格式必
须固定,而TDateTimePicker却没有设置时间与日期的格式的属
性.因此,我用TSQWDateTimePicker来实现此功能.
与Delphi的TDateTimePicker比较,TSQWDateTimePicker多出
一个属性DateTimeMask:string.当DateTimeMask为空时,其等
于TDateTimePicker,当DateTimeMask不为空时,时间与日期的
显示格式以DateTimeMask为准!
注意,DateTimeMask的格式与Delphi的FormatDateTime的Mask
有点区别:
年 : y,yy,yyy,yyyy (不能Y,YY,YYY,YYYY)
月 : M,MM(不能为m,mm)
日 : d,dd(不能为D)
时 : h,hh(12时). H,HH(24时)
分 : m,mm(不能为M,MM)
秒 : s,ss(不能为S,SS)
上午,下午: t,tt(不能为T,TT)
TSQWDateTimePicker的其他属性也应正确设置.
Good Luck!
注:
本控件(包括原代码)可以使用予任何目的(你应为你使用本控
件造成的任何后果负责).
}
unit DBDateTimePicker;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls,DB,dbctrls,Commctrl;
type
TDBDateTimePicker = class(TDateTimePicker)
private
{ Private declarations }
FDateTimeMask:string;
FFieldDataLink:TFieldDataLink;
procedure SetDateTimeMask(ADateTimeMask:string);
function GetReadOnly:boolean;
procedure SetReadOnly(Value:boolean);
function GetDataField:string;
procedure SetDataField(Value:string);
function GetDataSource:TDataSource;
procedure SetDataSource(Value:TDataSource);
function GetField:TField;
procedure CNNotify(var Message: TWMNotify);
message CN_NOTIFY;
procedure CMExit(var Message:TCMExit);message CM_EXIT;
protected
{ Protected declarations }
procedure CreateWnd;
override;
procedure FDataChange(Sender:TObject);
procedure FUpdateData(Sender:TObject);
procedure Notification(AComponent: TComponent;
Operation: TOperation);
override;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
property Field:TField read GetField;
published
{ Published declarations }
property DateTimeMask:string read FDateTimeMask write SetDateTimeMask;
property DataSource:TDataSource read GetDataSource write SetDataSource;
property DataField:string read GetDataField write SetDataField;
property ReadOnly:boolean read GetReadOnly write SetReadOnly;
// The Date, Time, ShowCheckbox, and Checked properties must be in this order:
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Data Controls', [TDBDateTimePicker]);
end;
{ TDBDateTimePicker }
procedure TDBDateTimePicker.CMExit(var Message: TCMExit);
begin
try
FFieldDataLink.UpdateRecord;
except
Self.SetFocus;
raise;
end;
inherited;
end;
procedure TDBDateTimePicker.CNNotify(var Message: TWMNotify);
begin
FFieldDataLink.Edit;
inherited;
FFieldDataLink.Modified;
end;
constructor TDBDateTimePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDateTimeMask:='';
FFieldDataLink:=TFieldDataLink.Create;
FFieldDataLink.ReadOnly:=False;
FFieldDataLink.Control:=Self;
FFieldDataLink.OnDataChange:=FDataChange;
FFieldDataLink.OnUpdateData:=FUpdateData;
end;
procedure TDBDateTimePicker.CreateWnd;
begin
inherited;
if FDateTimeMask<>'' then
SetDateTimeMask(FDateTimeMask);
end;
destructor TDBDateTimePicker.Destroy;
begin
FFieldDataLink.Free;
FFieldDataLink:=nil;
inherited;
end;
procedure TDBDateTimePicker.FDataChange(Sender: TObject);
begin
if (FFieldDataLink<>nil) and (FFieldDataLink.Field<>nil) then
begin
Self.Date:=Trunc(FFieldDataLink.Field.AsDateTime);
Self.Time:=Frac(FFieldDataLink.Field.AsDateTime);
end;
end;
procedure TDBDateTimePicker.FUpdateData(Sender: TObject);
begin
if (FFieldDataLink<>nil) and (FFieldDataLink.Field<>nil) then
FFieldDataLink.Field.AsDateTime:=TDateTime(Date);
end;
function TDBDateTimePicker.GetDataField: string;
begin
Result:=FFieldDataLink.FieldName;
end;
function TDBDateTimePicker.GetDataSource: TDataSource;
begin
Result:=FFieldDataLink.DataSource;
end;
function TDBDateTimePicker.GetField: TField;
begin
Result:=FFieldDataLink.Field;
end;
function TDBDateTimePicker.GetReadOnly: boolean;
begin
Result:=FFieldDataLink.ReadOnly;
end;
procedure TDBDateTimePicker.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent,Operation);
if (Operation=opReMove) and (FFieldDataLink<>nil) and
(AComponent=FFieldDataLink.DataSource) then
FFieldDataLink.DataSource:=nil;
end;
procedure TDBDateTimePicker.SetDataField(Value: string);
begin
FFieldDataLink.FieldName:=Value;
end;
procedure TDBDateTimePicker.SetDataSource(Value: TDataSource);
begin
FFieldDataLink.DataSource:=Value;
if Value<>nil then
Value.FreeNotification(Self);
end;
procedure TDBDateTimePicker.SetDateTimeMask(ADateTimeMask: string);
begin
FDateTimeMask:=ADateTimeMask;
if FDateTimeMask<>'' then
DateTime_SetFormat(Handle,PChar(FDateTimeMask))
else
RecreateWnd;
end;
procedure TDBDateTimePicker.SetReadOnly(Value: boolean);
begin
FFieldDataLink.ReadOnly:=Value;
end;
end.