荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Minatl (Minatl), 信区: Program
标  题: delphi显示系统时间
发信站: BBS 荔园晨风站 (Wed Jan 20 19:04:58 1999), 转信

    很多朋友都想在自己的程序中显示系统时间,这在DELPHI2.0、3.0中十分容易,利用
DateToStr(Date)及TimeToStr(Time)函数即可实现。二者的函数原型如下:
    functiomDateToStr(Date:TDateTime):string;
    functionTimeToStr(Time:TDateTime):string;
    其返回值均为String型。在程序中我们可以这样使用:
    Label1.Caption:=DateToStr(Date);
    Lable2.Caption:=TimeToStr(Time);
    二者分别调用了Delphi函数Date和Time读取系统日期和时间来实现的,但只能读系统时
钟,而不能设置系统时钟。那么如何处理这一问题呢?这正是本文所要讨论的问题。
    既然Delphi没有提供如此功能,但Delphi提供了调用WindowsAPI的接口。所以我们可以
调用WindowsAPI函数来实现这一功能。具体方法如下:
    procedureTForm1.Button1Click(Sender:TObject);
    begin
    Edit1.Text:='97/10/3010:09:59';//注意:控制面板内时间格式要为
YY/MM/DD
    end;
    procedureTForm1.Button2Click(Sender:TObject);
    var
    systemtime:Tsystemtime;
    DateTime:TDateTime;
    begin
    DateTime:=StrToDateTime(Edit1.text);
    DateTimeToSystemTime(DateTime,systemtime);
    SetLocalTime(SystemTime);
    GetLocalTime(SystemTime);//读取北京东8区时间
    DateTime:=SystemTimeToDateTime(SystemTime);
    Edit2.Text:ΚDateTimeToStr(DateTime);
    end;
    另外,还有好多其它的Delphi函数和API函数供我们使用,如:

StrToDate、StrToTime、DateTimeToStr、StrToDateTime、DateTimeToSystemTime、
SystemTimeToDateTime、DateTimeToTimeStamp、TimeStampToDateTimeCompareFileTime
、DosDateTimeToFileTime、FileTimeToDosDateTime、FileTimeToLocalFileTime、File
TimeToSystemTime、
GetFileTime、SetFileTime、GetSystemTime(格林威治时间)、SetSystemTimeGetSys
temTimeAdjustment,SetSystemTimdAdjustment。
    具体我们可以查Delphi所带的Win32.HLP(WindowsAPI帮助文件),它在Delphi2.0ι
Help或Delphi3ιHelp目录下。

--
※ 来源:.BBS 荔园晨风站 bbs.szu.edu.cn.[FROM: 192.168.0.240]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店