Recent

Author Topic: [SOLVED] XMLConfig window state  (Read 403 times)

Pe3s

  • Sr. Member
  • ****
  • Posts: 313
[SOLVED] XMLConfig window state
« on: September 22, 2022, 06:26:00 pm »
Hello forum members, how to properly write and read WindowState using XMLConfig
Regards :)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.   XMLConfigMain.SetDeleteValue('FormTop'   , IntToStr(Form1.Top)   , 'RestoredTop');
  4.   XMLConfigMain.SetDeleteValue('FormLeft'  , IntToStr(Form1.Left)  , 'RestoredLeft');
  5.   XMLConfigMain.SetDeleteValue('FormHeight', IntToStr(Form1.Height), 'RestoredHeight');
  6.   XMLConfigMain.SetDeleteValue('FormWidth' , IntToStr(Form1.Width) , 'RestoredWidth');
  7.   XMLConfigMain.SetDeleteValue('WindowState', IntToStr(Form1.WindowState), 'WindowState');
  8.  
  9.   XMLConfigMain.Flush;
  10.   XMLConfigMain.Free;
  11. end;
  12.  
  13. procedure TForm1.FormCreate(Sender: TObject);
  14. begin
  15.   XMLConfigMain := TXMLConfig.Create(nil);
  16.   XMLConfigMain.FileName := ChangeFileExt(Application.ExeName, '.xml');
  17.  
  18.   Top    := StrToInt(XMLConfigMain.GetValue('FormTop', '250'));
  19.   Left   := StrToInt(XMLConfigMain.GetValue('FormLeft', '550'));
  20.   Height := StrToInt(XMLConfigMain.GetValue('FormHeight', '350'));
  21.   Width  := StrToInt(XMLConfigMain.GetValue('FormWidth', '600'));
  22.   WindowState := TWindowState(StrToInt(XMLConfigMain.GetValue('WindowState', '0')));
  23. end;              
  24.  
« Last Edit: September 25, 2022, 04:59:26 pm by Pe3s »

paweld

  • Hero Member
  • *****
  • Posts: 525
Re: XMLConfig window state
« Reply #1 on: September 22, 2022, 07:16:07 pm »
You don't need to convert numbers to strings because XMLConfig supports writing and reading numbers.
---
Nie musisz konwertować liczb na ciąg ponieważ XMLConfig obsługuje zapis i odczyt liczb.
 
Code: Pascal  [Select][+][-]
  1. var
  2.   XMLConfigMain: TXMLConfig;
  3.  
  4. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  5. begin
  6.   XMLConfigMain.SetDeleteValue('FormTop', Form1.Top, 200);
  7.   XMLConfigMain.SetDeleteValue('FormLeft', Form1.Left, 200);
  8.   XMLConfigMain.SetDeleteValue('WindowState', ord(Form1.WindowState), 0);
  9.   XMLConfigMain.Flush;
  10.   XMLConfigMain.Free;
  11. end;
  12.  
  13. procedure TForm1.FormCreate(Sender: TObject);
  14. begin
  15.   XMLConfigMain := TXMLConfig.Create(nil);
  16.   XMLConfigMain.FileName := 'konfig.xml';
  17.   Top := XMLConfigMain.GetValue('FormTop', 200);
  18.   Left := XMLConfigMain.GetValue('FormLeft', 200);
  19.   WindowState := TWindowState(XMLConfigMain.GetValue('WindowState', 0));
  20. end;
Best regards / Pozdrawiam
paweld

Pe3s

  • Sr. Member
  • ****
  • Posts: 313
Re: XMLConfig window state
« Reply #2 on: September 22, 2022, 07:54:50 pm »
paweld Thank you

Pe3s

  • Sr. Member
  • ****
  • Posts: 313
Re: [SOLVED] XMLConfig window state
« Reply #3 on: September 23, 2022, 07:40:07 pm »
Hello, I have a question why WindowState is not working properly.
Jewzeli we will change the size of the form and save it, we will display it on the whole screen, it does not return to the previous settings

Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   cfg: TXMLConfig;
  4.  
  5. implementation
  6.  
  7. {$R *.lfm}
  8.  
  9. { TForm1 }
  10.  
  11. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  12. begin
  13.   with cfg do
  14.   begin
  15.      SetValue('NormalLeft', Left);
  16.      SetValue('NormalTop', Top);
  17.      SetValue('NormalWidth', Width);
  18.      SetValue('NormalHeight', Height);
  19.  
  20.      SetValue('RestoredLeft', RestoredLeft);
  21.      SetValue('RestoredTop', RestoredTop);
  22.      SetValue('RestoredWidth', RestoredWidth);
  23.      SetValue('RestoredHeight', RestoredHeight);
  24.  
  25.      SetValue('WindowState', Integer(WindowState));
  26.  
  27.      Flush;
  28.      Free;
  29.   end;
  30. end;
  31.  
  32. procedure TForm1.FormCreate(Sender: TObject);
  33. var
  34.   LastWindowState: TWindowState;
  35. begin
  36.   cfg := TXMLConfig.Create(Self);
  37.   with cfg do
  38.   begin
  39.     FileName := ChangeFileExt(Application.ExeName, '.xml');
  40.  
  41.      LastWindowState := TWindowState(GetValue('WindowState', Integer(WindowState)));
  42.  
  43.      if LastWindowState = wsMaximized then
  44.      begin
  45.        WindowState := wsNormal;
  46.        BoundsRect := Bounds(
  47.          GetValue('RestoredLeft', RestoredLeft),
  48.          GetValue('RestoredTop', RestoredTop),
  49.          GetValue('RestoredWidth', RestoredWidth),
  50.          GetValue('RestoredHeight', RestoredHeight));
  51.        WindowState := wsMaximized;
  52.      end else
  53.      begin
  54.         WindowState := wsNormal;
  55.         BoundsRect := Bounds(
  56.         GetValue('NormalLeft', Left),
  57.         GetValue('NormalTop', Top),
  58.         GetValue('NormalWidth', Width),
  59.         GetValue('NormalHeight', Height));
  60.      end;
  61.   end;
  62. end;
  63.  

Arioch

  • Sr. Member
  • ****
  • Posts: 414
Re: XMLConfig window state
« Reply #4 on: September 23, 2022, 07:47:36 pm »
What is TForm.Position ?

You most probably have to do it in the vicinity of Form.Show not Form.Create

look at how the existing libraries do implement it

notice that Create is pair to Destroy and NOT to Close
« Last Edit: September 23, 2022, 07:49:32 pm by Arioch »

Pe3s

  • Sr. Member
  • ****
  • Posts: 313
Re: XMLConfig window state
« Reply #5 on: September 25, 2022, 04:59:11 pm »
Thank you  :)

 

TinyPortal © 2005-2018