In this lesson you can learn how to work with INI files in Delphi.
You can learn how to save settings in application, like window position and size,
controls position and properties etc.
Here is video with all steps :
Here is also code used in this video :
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,IniFiles;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Panel1: TPanel;
Edit1: TEdit;
Button8: TButton;
CheckBox1: TCheckBox;
procedure Button7Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MyINI:TMemIniFile;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Panel1.top:=Panel1.Top-10;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Panel1.top:=Panel1.Top+10;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Panel1.left:=Panel1.left-10;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
Panel1.left:=Panel1.left+10;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
MyINI.EraseSection('Main');
MyINI.UpdateFile;
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
MyINI:=TMemIniFile.Create(ExtractFilePath(Application.ExeName)+'Settings.ini');
MyINI.WriteInteger('Main','Left',Form1.Left); //Main form left position on screen
MyINI.WriteInteger('Main','Top',Form1.Top); //Main form top position on screen
MyINI.WriteInteger('Main','Width',Form1.Width); //Main form width
MyINI.WriteInteger('Main','Height',Form1.Height); //Main form Height
MyINI.WriteInteger('Panel','Left',Panel1.Left); //Panel left position on screen
MyINI.WriteInteger('Panel','Top',Panel1.Top); //Panel top position on screen
MyINI.WriteString('Panel','Caption',Panel1.Caption); //Panel Caption
MyINI.WriteBool('Check','Checked',CheckBox1.Checked); //Checkbox checked
MyINI.UpdateFile;// Save settings to INI file
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button8Click(Sender: TObject);
begin
Panel1.Caption:=Edit1.Text;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyINI:=TMemIniFile.Create(ExtractFilePath(Application.ExeName)+'Settings.ini');
Form1.Left:=MyINI.ReadInteger('Main','Left',Form1.Left); //Main form left position on screen
Form1.Top:=MyINI.ReadInteger('Main','Top',Form1.Top); //Main form top position on screen
Form1.Width:=MyINI.ReadInteger('Main','Width',Form1.Width); //Main form width
Form1.Height:=MyINI.ReadInteger('Main','Height',Form1.Height); //Main form Height
Panel1.Left:=MyINI.ReadInteger('Panel','Left',Panel1.Left); //Panel left position on screen
Panel1.Top:=MyINI.ReadInteger('Panel','Top',Panel1.Top); //Panel top position on screen
Panel1.Caption:=MyINI.ReadString('Panel','Caption',Panel1.Caption); //Panel Caption
CheckBox1.Checked:=MyINI.ReadBool('Check','Checked',CheckBox1.Checked); //Checkbox checked
end;
end.
0 comments: