Recent

Author Topic: Correct events to restore width/height with non-default DPI  (Read 387 times)

anse

  • New Member
  • *
  • Posts: 10
Correct events to restore width/height with non-default DPI
« on: March 27, 2025, 09:51:51 am »
I'm still having some confusion with restoring the width and height of a form, especially when the form has DesignTimePPI=120 (not 96), and the app runs on a different DPI setting. Currently I am storing a user-defined size of a form in the OnDestroy event of a form:

Code: Pascal  [Select][+][-]
  1. procedure TMyForm.FormDestroy(Sender: Tobject);
  2. begin
  3.   SaveToJson('TMyForm.Width', ScaleFormToDesign(Width));
  4.   SaveToJson('TMyForm.Height', ScaleFormToDesign(Height));
  5. end;
  6.  
  7. procedure TMyForm.FormCreate(Sender: TObject);
  8. begin
  9.   // Restoring size
  10.   Width := ReadFromJson('TMyForm.Width');
  11.   Height := ReadFromJson('TMyForm.Height');
  12. end;
  13.  

Is that the right approach when I want to support DPI scaling across platforms, plus consider a value in TMyForm.DesignTimePPI <> 96?
Does it make a difference whether I store the width/height on OnDestroy or OnClose - or in other words - is a form "DeScaled" in OnDestroy? I guess no.

anse

  • New Member
  • *
  • Posts: 10
Re: Correct events to restore width/height with non-default DPI
« Reply #1 on: March 29, 2025, 05:03:03 pm »
To answer my own question: yes, that approch is working correctly.

Here's a screen of a test project
* designed on Windows @120ppi
* then copied to my Linux box
* built with /usr/bin/lazbuild (without touching the form properties)
* then running on Linux @96ppi.

Observations:
* DesignTimePPI is 120, and never changes at runtime
* PixelsPerInch was 120 at designtime, changes at runtime to the value of Screen.PixelsPerInch
* FormCreate sets width and height to 500px (still in 120ppi as designed!)
* LCL scaling takes place (where exactly?)
* in FormShow, the width is now 400px (96ppi)
* In FormDestroy the form is not descaled as I suspected
* Instead, storing the width for the next session using ScaleFormToDesign(Width) scales it up from 400px to 500px
* Next time the app runs it sets the width again to 500px, and the scaling again decreases that to 400px

Just feels a bit wrong to store dimensions in the scaling the developer dictates, instead of the actual user scaling. But that's rather philosophic I suppose.
« Last Edit: March 30, 2025, 11:00:43 am by anse »

 

TinyPortal © 2005-2018