A class that manages recently used files

zhaozj2021-02-16  87

A class that manages recently used files:

{------------------------------------- ---------------------------- Unit name: rcntfilemgr author:

Purpose: Manager The Recent File List. History: 2004.06.08 create ------------------------------------ -----------------------------------------}

Unit rcntfilemgr;

Interface

Uses classes, sysutils, inifiles;

type TRecentFileChangedEvent = procedure (Sender: TObject) of object; TRecentFileManager = class (TObject) private FRecentFileList: TStringList; FMaxRecentCount: Integer; FOnRecentFileChanged: TRecentFileChangedEvent; protected function GetRecentFileCount (): Integer; function GetRecentFile (Index: Integer): String; procedure LoadFromConfigFile (); procedure SaveToConfigFile (); public constructor Create (); destructor Destroy (); override; procedure AddRecentFile (const aFileName: String); property RecentFileCount: Integer read GetRecentFileCount; property RecentFile [Index: Integer]: String read GetRecentFile; Property OnRecentFilechanged: TreatfileChangeDevent Read FonRecentFileChanged Write FonRecentFileChanged; End; Implementation

{TreatfileManager}

Function TreatFileManager.getRecentFileCount (): integer; begin result: = FRECENTFILIST.COUNT; END;

Function TreatFileManager.getRecentfile (index: integer): string; begin result: = FRECENTFILELIST.STRINGS [INDEX];

procedure TRecentFileManager.LoadFromConfigFile (); var Ini: TInifile; KeyList: TStringList; I: Integer; begin Ini: = TInifile.Create (ExtractFilePath (ParamStr (0)) 'config.ini'); KeyList: = TStringList.Create ( ); Try ini.readsection ('Recentfile', Keylist); for i: = 0 to keylist.count-1 do begin freultfilelist.add (INI.ReadString ('Recentfile', Keylist.Strings [i], '')) ; end; if Assigned (FOnRecentFileChanged) then begin FOnRecentFileChanged (self); end; finally Ini.Free; KeyList.Free; end; end; procedure TRecentFileManager.SaveToConfigFile (); var Ini: TInifile; I: Integer; begin Ini: = Tinifile.create (ExtractFilePath) 'config.ini'); Try Ini.erasection ('Recentfile'); for i: = 0 to FractfileList.count-1 Do Begin Ini.WritString ('Recentfile', 'Recent' INTOSTR (I), FRECENTFILELIST.STRINGS [I]); end; finally ini.free; end; end;

Constructor trecentfilemanager.create (); begin inherited create (); frecentfilelist: = tstringlist.create (); fMaxRecentcount: = 5; loadingfromconfigfile (); end;

destructor TRecentFileManager.Destroy (); begin if Assigned (FRecentFileList) then begin try SaveToConfigFile (); except // ignore any exceptions end; FreeAndNil (FRecentFileList); end; inherited Destroy (); end;

procedure TRecentFileManager.AddRecentFile (const AFileName: String); var RecentIndex: Integer; begin RecentIndex: = FRecentFileList.IndexOf (AFileName); if RecentIndex> = 0 then begin FRecentFileList.Delete (RecentIndex); end; FRecentFileList.Insert (0, AFileName ); while FRecentFileList.Count> FMaxRecentCount do begin FRecentFileList.Delete (FRecentFileList.Count-1); end; if Assigned (FOnRecentFileChanged) then begin FOnRecentFileChanged (self); end; end; end.

转载请注明原文地址:https://www.9cbs.com/read-11955.html

New Post(0)