Recent

Author Topic: Read INI  (Read 2736 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Read INI
« on: November 08, 2021, 06:59:49 pm »
Hello, I found the code on the internet
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SaveSettings;
  2. begin
  3.   myINI := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  4.   try
  5.     Assert(myINI <> nil);
  6.     if HandleAllocated then
  7.     begin
  8.       Wp.Length := SizeOf(TWindowPlacement);
  9.       GetWindowPlacement(Handle, @Wp);
  10.  
  11.       myINI.WriteInteger('Placement', 'Top', Wp.rcNormalPosition.Top);
  12.       myINI.WriteInteger('Placement', 'Left', Wp.rcNormalPosition.Left);
  13.       myINI.WriteInteger('Placement', 'Width', Wp.rcNormalPosition.Right - Wp.rcNormalPosition.Left);
  14.       myINI.WriteInteger('Placement', 'Height', Wp.rcNormalPosition.Bottom - Wp.rcNormalPosition.Top);
  15.  
  16.       myINI.WriteBool('Placement', 'Maximized', WindowState = wsMaximized);
  17.     end;
  18.   finally
  19.     myINI.Free;
  20.   end;
  21. end;
how can i read these settings
Thank you
« Last Edit: November 08, 2021, 08:00:28 pm by Pe3s »

ezlage

  • New Member
  • *
  • Posts: 32
  • Silence is the perfect beat!
    • GitHub

ArtLogi

  • Full Member
  • ***
  • Posts: 184
Re: Read INI
« Reply #2 on: November 08, 2021, 10:21:46 pm »
You can also do it with RTL functions (as filehandling and string functions) and dynamic arrays, but that is the hard way, to get everything trapped on events and more so without bugs, when there is a ready made tool for ini-files.

I can not recommend... only advantage is that it is easier to remember the primitive functions than advanced library etc.
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Read INI
« Reply #3 on: November 09, 2021, 07:40:26 am »
I quickly written a short demo:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, StdCtrls, IniFiles;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btnLoad: TButton;
  16.     btnSave: TButton;
  17.     chkAutosave: TCheckBox;
  18.     procedure btnLoadClick(Sender: TObject);
  19.     procedure btnSaveClick(Sender: TObject);
  20.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.     procedure SavePosition;
  24.     procedure LoadPosition;
  25.   end;
  26.  
  27. const
  28.   ConfigFile = 'config.ini';
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  40. begin
  41.   if chkAutosave.Checked then
  42.     SavePosition;
  43. end;
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. begin
  47.   LoadPosition;
  48. end;
  49.  
  50. procedure TForm1.btnLoadClick(Sender: TObject);
  51. begin
  52.   LoadPosition;
  53. end;
  54.  
  55. procedure TForm1.btnSaveClick(Sender: TObject);
  56. begin
  57.   SavePosition;
  58. end;
  59.  
  60. procedure TForm1.SavePosition;
  61. var
  62.   Config: TIniFile;
  63. begin
  64.   Config := TIniFile.Create(ConfigFile);
  65.   try
  66.     Config.WriteInteger('Position', 'Top', Self.Top);
  67.     Config.WriteInteger('Position', 'Left', Self.Left);
  68.     Config.WriteInteger('Position', 'Width', Self.Width);
  69.     Config.WriteInteger('Position', 'Height', Self.Height);
  70.   finally
  71.     Config.Free;
  72.   end;
  73. end;
  74.  
  75. procedure TForm1.LoadPosition;
  76. var
  77.   Config: TIniFile;
  78. begin
  79.   Config := TIniFile.Create(ConfigFile);
  80.   try
  81.     Self.Top    := Config.ReadInteger('Position', 'Top', 100);
  82.     Self.Left   := Config.ReadInteger('Position', 'Left', 100);
  83.     Self.Width  := Config.ReadInteger('Position', 'Width', 320);
  84.     Self.Height := Config.ReadInteger('Position', 'Height', 240);
  85.   finally
  86.     Config.Free;
  87.   end;
  88. end;
  89.  
  90. end.

'Self' in the code above can be safely removed, but to make it clear (for newbies), I kept them.
« Last Edit: November 09, 2021, 07:41:59 am by Handoko »

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: Read INI
« Reply #4 on: November 09, 2021, 08:09:44 am »
@Handoko Thank you for the demo.  the problem with ini is that I wanted to make the program remember the form size setting additionally as we maximize the form so that it can write and read.  after returning to the normal window to return to the established dimensions.

Ally

  • Jr. Member
  • **
  • Posts: 53
Re: Read INI
« Reply #5 on: November 09, 2021, 10:06:04 am »
Hello Pe3s,

with RestoredTop ... and WindowState it works.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, StdCtrls, IniFiles;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btnClose: TButton;
  16.     procedure btnCloseClick(Sender: TObject);
  17.     procedure FormShow(Sender: TObject);
  18.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. const
  26.   ConfigFile = 'config.ini';
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.btnCloseClick(Sender: TObject);
  38. begin
  39.   Close;
  40. end;
  41.  
  42. procedure TForm1.FormShow(Sender: TObject);
  43. var
  44.   Config: TIniFile;
  45. begin
  46.   Config := TIniFile.Create(ConfigFile);
  47.   try
  48.     Top := Config.ReadInteger('Position', 'Top', 100);
  49.     Left := Config.ReadInteger('Position', 'Left', 100);
  50.     Width := Config.ReadInteger('Position', 'Width', 320);
  51.     Height := Config.ReadInteger('Position', 'Height', 240);
  52.     WindowState := TWindowState(Config.ReadInteger('Position', 'WindowState', 0));
  53.   finally
  54.     Config.Free;
  55.   end;
  56. end;
  57.  
  58. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  59. var
  60.   Config: TIniFile;
  61. begin
  62.   Config := TIniFile.Create(ConfigFile);
  63.   try
  64.     Config.WriteInteger('Position', 'Top', RestoredTop);
  65.     Config.WriteInteger('Position', 'Left', RestoredLeft);
  66.     Config.WriteInteger('Position', 'Width', RestoredWidth);
  67.     Config.WriteInteger('Position', 'Height', RestoredHeight);
  68.     Config.WriteInteger('Position', 'WindowState', Integer(WindowState));
  69.   finally
  70.     Config.Free;
  71.   end;
  72. end;
  73.  
  74. end.
« Last Edit: November 09, 2021, 10:11:21 am by Ally »

Pe3s

  • Hero Member
  • *****
  • Posts: 533
[SOLVED] Re: Read INI
« Reply #6 on: November 09, 2021, 05:56:32 pm »
Thank you, that's what I meant.
regards

Tony Stone

  • Full Member
  • ***
  • Posts: 219
Re: Read INI
« Reply #7 on: November 20, 2021, 01:24:23 am »
May be worth taking a look at sessionProperties as well.

https://wiki.lazarus.freepascal.org/TXMLPropStorage

@Handoko Thank you for the demo.  the problem with ini is that I wanted to make the program remember the form size setting additionally as we maximize the form so that it can write and read.  after returning to the normal window to return to the established dimensions.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1122
  • Professional amateur ;-P
Re: Read INI
« Reply #8 on: November 20, 2021, 01:52:22 am »
Hey Pe3s,

The code you found is a tad hands on and there's loads you have to do by hand, which can be error prone.

Like @TonyStone mentioned, a better way of doing it is using the T[XML|INI|JSON]PropStorage.
Opting for any of those, you can have your settings automatically loaded at start up and automatically stored upon close.
And in one of 3 flavours: XML, INI, JSON.
The component even has a generic "bag" of options you can store that are not RTTI related.

I tend to use the JSON one(Disclaimer: I'm the author and asked the dev to include it on the sources) since I had an itch to scratch and I did scratch it :)

Hope this gives you a bit more options.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Pe3s

  • Hero Member
  • *****
  • Posts: 533
[SOLVED] Re: Read INI
« Reply #9 on: November 20, 2021, 12:40:35 pm »
Thank you

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1122
  • Professional amateur ;-P
Re: [SOLVED] Re: Read INI
« Reply #10 on: November 20, 2021, 10:44:30 pm »
Hey Pe3s,

Thank you

Quite welcome my fellow Pascal enthusiast.

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

 

TinyPortal © 2005-2018