Mega Code Archive

 
Categories / Delphi / Examples
 

Inifiles

HERE WE HAVE A WHOLE PROGRAM THAT WILL CREATE AN INI FILE (in the Windows directory) and WRITE POSITION INFORMATION FOR THE PROGRAM'S MAIN FORM TO THE INI FILE (so that the second time you run the program the form is placed at it's last position)... See ReadINIFile() and WriteINIFile() routines below -best way to do it... ************************************************************************* So, if you want to use INI files, remember to put ini file code IN A FORM'S MAIN UNIT so that i) when the form loads it reads the ini file, and ii) when the form closes it writes to the ini file: eg: const iniFileName = 'Karma.INI'; type TSrchForm = class(TForm) procedure ReadINIFile; procedure WriteINIFile; private end; procedure TSrchForm.FormCreate(Sender: TObject); begin ReadINIFile; end; procedure TSrchForm.FormClose(Sender: TObject; var Action: TCloseAction); begin WriteIniFile; end; procedure TRandForm.ReadINIFile; var thisProgIni: TIniFile; begin thisProgIni := TIniFile.Create(iniFileName); with thisProgIni do begin try RandForm.Left := ReadInteger('Position', 'Left', 0); RandForm.Top := ReadInteger('Position', 'Top', 0); finally {and free up memory used in creating the INI file...} Free; end; end; {pathToExe := ExtractFilePath(exeFileName);} end; procedure TRandForm.WriteINIFile; var thisProgIni: TIniFile; begin thisProgIni := TIniFile.Create(iniFileName); with thisProgIni do begin try WriteInteger('Position', 'Left', RandForm.Left); WriteInteger('Position', 'Top', RandForm.Top); finally {free up memory used in creating the INI file (again)...} Free; end; end; end; ************************************************************ unit IniUnit; {Richard Dec '98} {tiny prog to demonstrate the writing of an INI file (to keep track of last screen position of a form, for instance) -note the addition of IniFiles to the 'uses' clause...} interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, IniFiles, StdCtrls; type TMainForm = class(TForm) ExitButton: TButton; Label1: TLabel; procedure ExitButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private {private declarations} public {public declarations} end; var MainForm: TMainForm; implementation {$R *.DFM} procedure TMainForm.ExitButtonClick(Sender: TObject); begin Close; end; procedure TMainForm.FormCreate(Sender: TObject); var thisProgIni: TIniFile; exeFileName, iniFileName: String; begin exeFileName := Application.ExeName; {get the filename without path information...} exeFileName := ExtractFileName(exeFileName); {and change the extension to .INI ...} iniFileName := ChangeFileExt(exeFileName, '.INI'); thisProgIni := TIniFile.Create(iniFileName); with thisProgIni do begin try MainForm.Left := ReadInteger('Position', 'Left', 0); MainForm.Top := ReadInteger('Position', 'Top', 0); MainForm.Width := ReadInteger('Position', 'Width', 0); MainForm.Height := ReadInteger('Position', 'Height', 0); finally {and free up memory used in creating the INI file...} Free; end; end; {pathToExe := ExtractFilePath(exeFileName);} end; procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction); var thisProgIni: TIniFile; exeFileName, iniFileName: String; begin exeFileName := Application.ExeName; {get the filename without path information...} exeFileName := ExtractFileName(exeFileName); {and change the extension to .INI ...} iniFileName := ChangeFileExt(exeFileName, '.INI'); thisProgIni := TIniFile.Create(iniFileName); with thisProgIni do begin try WriteInteger('Position', 'Left', MainForm.Left); WriteInteger('Position', 'Top', MainForm.Top); WriteInteger('Position', 'Width', MainForm.Width); WriteInteger('Position', 'Height', MainForm.Height); finally {free up memory used in creating the INI file (again)...} Free; end; end; end; end. ****************************************************************** make sure the paths are correct by using Application.ExeName, etc: procedure TMainForm.ReadINIFile; var thisProgIni: TIniFile; exeFileName, exeFilePath: String; begin exeFileName := Application.ExeName; ExeFilePath := ExtractFilePath(exeFileName); thisProgIni := TIniFile.Create(ExeFilePath + FormsIniFileName); with thisProgIni do begin try ThumbNailForm.Left := ReadInteger('ThumbNailFormPosition', 'Left', 0); ThumbNailForm.Top := ReadInteger('ThumbNailFormPosition', 'Top', 0); ProgressFloatForm.Left := ReadInteger('ProgressFloatFormPosition', 'Left', 0); ProgressFloatForm.Top := ReadInteger('ProgressFloatFormPosition', 'Top', 0); finally {and free up memory used in creating the INI file...} Free; end; end; end; procedure TMainForm.WriteINIFile; var thisProgIni: TIniFile; exeFileName, exeFilePath: String; begin exeFileName := Application.ExeName; ExeFilePath := ExtractFilePath(exeFileName); thisProgIni := TIniFile.Create(ExeFilePath + FormsIniFileName); with thisProgIni do begin try WriteInteger('ThumbNailFormPosition', 'Left', ThumbNailForm.Left); WriteInteger('ThumbNailFormPosition', 'Top', ThumbNailForm.Top); WriteInteger('ProgressFloatFormPosition', 'Left', ProgressFloatForm.Left); WriteInteger('ProgressFloatFormPosition', 'Top', ProgressFloatForm.Top); finally {free up memory used in creating the INI file (again)...} Free; end; end; end; *********************************************************************** If you want to overcome the 64k size limit on INI files: Try using TBigIni. It a component written, by somebody (I can't remeber right now, sorry) to overcome the 64K limit on INI files. But since it does not use the Win32 functions, it should work. It also is pretty much of a drop in, if you are using the TINIFile component. James.