以下代码利用TIniFile实现窗体打开时读取用户之前的时间选择、窗体关闭时保存用户最新的时间选择以供下次打开窗体时读取 的功能。
procedure TForm1.FormShow(Sender: TObject); VAR filename:string; myini:TIniFile; begin filename:=GetCurrentDir+'myini.txt'; myini:=TIniFile.Create(filename); wwDBDateTimePicker1.DateTime:=Now;//真正在系統中時,要使用數據庫中的時間! //ShowMessage(DateToStr(wwDBDateTimePicker1.DateTime));//彈出'2021/5/7'。 //ShowMessage(DateTimeToStr(wwDBDateTimePicker1.DateTime));//彈出'2021/5/7 上午 10:18:23'。 //注意DateToStr和DateTimeToStr是不一樣的!! //且StrToDate和StrToDateTime也是不一樣的!! //wwDBDateTimePicker1.DateTime:=StrToDate(myini.ReadString('UserSet','time',DateToStr(wwDBDateTimePicker1.DateTime)));//報錯!無論如何都報 //錯。因為保存時Write的是DateTime,,,所以用DateTime就要一致全部用DateTime! wwDBDateTimePicker1.DateTime:=StrToDateTime(myini.ReadString('NewUserSet','time',DateTimeToStr(wwDBDateTimePicker1.DateTime))); //wwDBDateTimePicker1.DateTime:=myini.ReadDateTime('UserSet','time',wwDBDateTimePicker1.DateTime); myini.Free; end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); VAR filename:string; myini:TIniFile; begin filename:=GetCurrentDir+'myini.txt'; myini:=TIniFile.Create(filename); //myini.WriteDateTime('UserSet','time',wwDBDateTimePicker1.DateTime); //myini.DeleteKey('UserSet','Time');//把之前的key刪掉(匹配不區分大小寫) //myini.EraseSection('UserSet');//把之前的小結刪掉 myini.WriteDateTime('NewUserSet','Time',wwDBDateTimePicker1.DateTime);//当这个TXT不存在时,这句会在
//当前filename路径下创建一个myini.txt!
myini.Free;
end;