增强DELPHI日期控件DATETIMEPICKER的功能,分享心得,希望对大家有用(0分)

Z

zwfyazl

Unregistered / Unconfirmed
GUEST, unregistred user!
DATATIMEPICKER控件,以下简称DTP,一直不是很好用,经过多日研究,现在提升了dtp的功能,具体功能和实现方法描述如下:
功能实现如下:
1、可以实现日期和时间的“年度”输入完以后自动跳转到“月份”,依次类推
2、可以实现日期和时间同时正确输入
准备工作及说明:
1、放置DTP控件,并把DTP的name设置为dtp1,kind属性设置为dtKTime
2、设置dtp的format属性位:yyyy-MM-dd HH:mm:ss 目的是实现日期和时间的输入,如果仅仅是输入日期或者时间,可以自行修改
3、在程序模块设置几个公共变量,放入程序头部public环节:
public
olddt,newdt:tdatetime;
dtk:word;
4、程序主要有3个部分,Formcreat模块初始公共变量,dtp控件onkeydown事件,dtp控件的onchange事件

主要程序如下:
procedure TForm1.FormCreate(Sender: TObject);
begin
olddt:=dtp1.DateTime;
dtk:=999;
end;

procedure TForm1.dtp1KeyDown(Sender: TObject;
var Key: Word;

Shift: TShiftState);
begin
dtk:=key;
end;

procedure TForm1.dtp1Change(Sender: TObject);
var
Buffer: PChar;
startloc,endloc,Size: Byte;
month1,day1,year1:word;
month2,day2,year2:word;
HOUR1,min1,sec1,msec1:word;
HOUR2,min2,sec2,msec2:word;
begin
Size := dtp1.GetTextLen;
Inc(Size);
GetMem(Buffer, Size);
dtp1.GetTextBuf(Buffer,Size);
DTP1.DateTime :=STRTODATETIME(StrPas(Buffer));
FreeMem(Buffer, Size);
newdt:=dtp1.DateTime;
if (dtk<>38)and (dtk<>40) then
begin
IF DTK<>999 then
begin
Decodedate(olddt,year1,month1,day1);
Decodedate(newdt,year2,month2,day2);
if year1<>year2 then
Sendmessage(dtp1.handle,WM_KeyDown,VK_right,0);
if month1<>month2 then
Sendmessage(dtp1.handle,WM_KeyDown,VK_right,0);
if day1<>day2 then
Sendmessage(dtp1.handle,WM_KeyDown,VK_right,0);
Decodetime(olddt,hour1,min1,sec1,msec1);
Decodetime(newdt,hour2,min2,sec2,msec2);
if hour1<>hour2 then
Sendmessage(dtp1.handle,WM_KeyDown,VK_right,0);
if min1<>min2 then
Sendmessage(dtp1.handle,WM_KeyDown,VK_right,0);
if sec1<>sec2 then
Sendmessage(dtp1.handle,WM_KeyDown,VK_right,0);
if msec1<>msec2 then
Sendmessage(dtp1.handle,WM_KeyDown,VK_right,0);
end;
end;
DTK:=999;
olddt:=newdt;
end;
 
Z

zwfyazl

Unregistered / Unconfirmed
GUEST, unregistred user!
欢迎大家多提宝贵意见
 
J

jack2004

Unregistered / Unconfirmed
GUEST, unregistred user!
为何不封装成新的控件呢?
 
顶部