Recent

Author Topic: How to save the Normal Size of the Form to the configuration file  (Read 4892 times)

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Hi, everyone, I want to save the Normal Size of the Form to the configuration file when exiting the program, but if the Form is Maximized when you exit the program, you will not save the Normal Size, but the Maximized Size, Does anyone know how to deal with this situation?
« Last Edit: October 30, 2017, 03:21:58 am by tomitomy »

PatBayford

  • Full Member
  • ***
  • Posts: 125
Re: How to save the Normal Size of the Form to the configuration file
« Reply #1 on: October 28, 2017, 06:00:16 am »
Well, one obvious way would be to have a pair of properties to store the normal size.
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to save the Normal Size of the Form to the configuration file
« Reply #2 on: October 28, 2017, 07:16:18 am »
Well, one obvious way would be to have a pair of properties to store the normal size.

Thank you, PatBayford. But I don't know how to get the Normal Size when the Form Maximizing.

Test Code:
Code: Pascal  [Select][+][-]
  1. var
  2.   NormalRect: TRect;
  3.  
  4. procedure SaveFormSize(Rect: TRect);
  5. begin
  6.   writeln(Format('Form Size Saved: %d, %d, %d, %d', [NormalRect.Left, NormalRect.Top, NormalRect.Width, NormalRect.Height]));
  7. end;
  8.  
  9. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  10. begin
  11.   if WindowState = wsNormal then
  12.     NormalRect := BoundsRect;
  13.   SaveFormSize(NormalRect);
  14. end;
  15.  
  16. procedure TForm1.FormWindowStateChange(Sender: TObject);
  17. begin
  18.   if WindowState <> wsNormal then begin
  19.     NormalRect := BoundsRect;  // How to get Normal Size
  20.   end;
  21. end;
« Last Edit: October 28, 2017, 07:52:31 am by tomitomy »

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: How to save the Normal Size of the Form to the configuration file
« Reply #3 on: October 29, 2017, 02:38:02 am »
restoreLeft, restoreTop, RestoreWidth and RestoreHeight

These are part of the form
 
 I do not know why they didn't simply return a BoundsRect or simply a Trect here ?
The only true wisdom is knowing you know nothing

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to save the Normal Size of the Form to the configuration file
« Reply #4 on: October 29, 2017, 03:14:51 am »
restoreLeft, restoreTop, RestoreWidth and RestoreHeight

These are part of the form
 
 I do not know why they didn't simply return a BoundsRect or simply a Trect here ?

I'm sorry, I didn't know the attributes like "restoreLeft" before. Thank you for helping me!

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Hi, I have tried "restoredLeft, restoredTop, restoredWidth, restoredHeight", but it doesn't work, Is there anything wrong with my usage?

Test Code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormWindowStateChange(Sender: TObject);
  2. begin
  3.   writeln(WindowState, ' : ',
  4.     Format('%d, %d, %d, %d',
  5.     [restoredLeft, restoredTop,
  6.     restoredWidth, restoredHeight]));
  7. end;


engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to save the Normal Size of the Form to the configuration file
« Reply #6 on: October 30, 2017, 05:50:40 am »
Hi, I have tried "restoredLeft, restoredTop, restoredWidth, restoredHeight", but it doesn't work, Is there anything wrong with my usage?

Test Code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormWindowStateChange(Sender: TObject);
  2. begin
  3.   writeln(WindowState, ' : ',
  4.     Format('%d, %d, %d, %d',
  5.     [restoredLeft, restoredTop,
  6.     restoredWidth, restoredHeight]));
  7. end;

Your usage seems ok and it works fine here on Windows. The GIF shows that both RestoredTop and RestoredLeft hold right values.

I doubt it, but maybe you need to be outside the event. Try something like:
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     ...
  3.     procedure StateChanged(AData:PtrInt);
  4.  
  5. ...
  6.  
  7. procedure TForm1.FormWindowStateChange(Sender: TObject);
  8. begin
  9.   Application.QueueAsyncCall(@StateChanged, 0);
  10. end;
  11.  
  12. procedure TForm1.StateChanged(AData: PtrInt);
  13. begin
  14.   writeln(WindowState, ' : ',
  15.     Format('%d, %d, %d, %d',
  16.     [restoredLeft, restoredTop,
  17.     restoredWidth, restoredHeight]));
  18. end;

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to save the Normal Size of the Form to the configuration file
« Reply #7 on: October 30, 2017, 07:40:43 am »
I doubt it, but maybe you need to be outside the event. Try something like:

Thank you, engkin, My OS is Linux Mint MATE, I tried your code, but the result was the same.

I also tested the code in Win7 virtual machine, everything is fine.

It looks like a BUG.


engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to save the Normal Size of the Form to the configuration file
« Reply #8 on: October 31, 2017, 01:01:26 am »
I tried your code, but the result was the same.

It is not the same. Your second GIF shows that you get wrong values when the form is maximized.

It looks like a BUG.

I agree.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: How to save the Normal Size of the Form to the configuration file
« Reply #9 on: October 31, 2017, 01:23:19 am »
I just did a test on 1.6.4 64 bits windows.

They do work however, I noticed they are late on defining their values.

 for example, If I implement the OnSize event for the Tform, the first OnSize event trigger shows that
theses values are all zero. But they are defined after the OnSize event, meaning, if I Maximize the form that
same OnSize event will get called again and the values are now assigned.

 And if I size the form to see that it has enlarged 1 pixel for example, then do the Maximize again, I can
see that it actually sized two pixels but because these values are getting updated after the Onsize event makes it
hard to track accurate sizes if you are dragging the form size around.

procedure TForm1.FormResize(Sender: TObject);
begin
  Caption := RestoredLeft.ToString+','+RestoredTop.ToString+','+RestoredWidth.ToString+','+RestoredHeight.ToString;
end;

Try this.
 Initial OnSize shows 0,0,0,0;
Then do something like maximize and you'll see that they now show the restore values.
then Drag the form size so something different, you'll see they are changing but then do a maximize and you'll
see the values will to a slightly different value..

This proves it is doing late settings after the OnSize event.
                     
The only true wisdom is knowing you know nothing

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to save the Normal Size of the Form to the configuration file
« Reply #10 on: October 31, 2017, 02:40:05 am »
Thank you engkin and jamie.

I have submitted the issue to the bug tracker forum:
https://bugs.freepascal.org/view.php?id=32631

 

TinyPortal © 2005-2018