Recent

Author Topic: Saving and restoring the state of a form at run-time, anyone?  (Read 4612 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 398
Saving and restoring the state of a form at run-time, anyone?
« on: December 24, 2021, 06:52:09 am »
I have an app with many forms and I would like to save and restore the state of that app, which means saving and restoring the state of each form. I can certainly write the code to do that but wanted to check and see if that's been done already.

Thanks for any feedback.


af0815

  • Hero Member
  • *****
  • Posts: 1409
Re: Saving and restoring the state of a form at run-time, anyone?
« Reply #1 on: December 24, 2021, 09:05:30 am »
What is the understanding of 'state of the form' ?

Do you want this https://wiki.freepascal.org/TXMLPropStorage ?
regards
Andreas

zoltanleo

  • Hero Member
  • *****
  • Posts: 522
Re: Saving and restoring the state of a form at run-time, anyone?
« Reply #2 on: December 24, 2021, 09:22:27 am »
I have an app with many forms and I would like to save and restore the state of that app, which means saving and restoring the state of each form. I can certainly write the code to do that but wanted to check and see if that's been done already.

Thanks for any feedback.
I recommend JSON Tools for Pascal (https://forum.lazarus.freepascal.org/index.php/topic,46533.0.html)
Win10 LTSC x64/LMDE7-x11-x86_64(gtk2/gtk3)/Fedora-44-wayland-x86_64(gtk2/gtk3)/ Kubuntu-24-x11-x86_64(qt5/qt6)/KDE Neon-wayland-x86_64(qt5/qt6)/Darwin Cocoa x86_64 (15.7.7):
Lazarus x32_64 (trunk); FPC(trunk), FireBird 5.0.3 x86_64; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

jcmontherock

  • Sr. Member
  • ****
  • Posts: 356
Re: Saving and restoring the state of a form at run-time, anyone?
« Reply #3 on: December 24, 2021, 04:58:31 pm »
For me:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SaveParms;
  2. var
  3.   FS:         TFileStream;
  4.   Writer:     TWriter;
  5.   FormRect:   TRect;
  6.   sFilename:  String;
  7. begin
  8.   sFilename := ExtractFilePath(ParamStr(0)) + ChangeFileExt({$I %FILE%}, '.parm');
  9.   FS := TFileStream.Create(sFilename, fmCreate);
  10.   Writer := TWriter.Create(FS, 1024); // Buffer size 1024
  11.   FormRect := Bounds(Form1.Left, Form1.Top, Form1.Width, Form1.Height);
  12.   Writer.Write(FormRect, SizeOf(FormRect));
  13.   Writer.WriteInteger(Integer(Form1.WindowState));
  14.   Writer.Free;
  15.   FS.Free;
  16. end;
  17.  
  18. procedure TForm1.ReadParms;
  19. var
  20.   FS:         TFileStream;
  21.   Reader:     TReader;
  22.   FormRect:   TRect;
  23.   sFilename:  String;
  24. begin
  25.   sFilename := ExtractFilePath(ParamStr(0)) + ChangeFileExt({$I %FILE%}, '.parm');
  26.   if FileExists(sFilename) = False then Exit;
  27.   FS := TFileStream.Create(sFilename, fmOpenRead);
  28.   Reader := TReader.Create(FS, 1024);
  29.   Reader.Read(FormRect, SizeOf(FormRect));
  30.   Form1.SetBounds(FormRect.Left, FormRect.Top, FormRect.Width, FormRect.Height);
  31.   Form1.WindowState := TWindowState(Reader.ReadInteger);
  32.   Reader.Free;
  33.   FS.Free;
  34. end;
  35.  
  36.  
Windows 11 UTF8-64 - Lazarus 4.6-64 - FPC 3.2.2

 

TinyPortal © 2005-2018