* * *

Author Topic: [Solved] Saving Window position and High DPI  (Read 464 times)

lainz

  • Hero Member
  • *****
  • Posts: 2774
  • I'm coding :)
    • Lainz
[Solved] Saving Window position and High DPI
« on: May 02, 2018, 07:29:13 pm »
Hi,

I'm with this problem, I can't solve.

I store the properties of the Form with JSONPropStorage. I store left, top, width and height.

I'm using Lazarus trunk, and I set Use LCL Scaling (hi-dpi) to true in the options.

The problem is that every time I open the window, it gets bigger.

As I understand the problem, it loads the stored values and then scales it. The next time does the same, loads the stored values and scales them.

How I can prevent this to happen?

I've set the properties to store (left, top, width, height) with the object inspector (SessionProperties).

I don't know how I can solve it. The DPI can be changed also, I'm running now at 120%, but when I switch to another mode for any reason, It will stop working as well.
« Last Edit: May 02, 2018, 09:43:02 pm by lainz »

GetMem

  • Hero Member
  • *****
  • Posts: 3083
Re: Saving Window position and High DPI
« Reply #1 on: May 02, 2018, 07:52:36 pm »
Did you try to scale with MulDiv from LCLType?
Code: Pascal  [Select]
  1. WidthX% := MulDiv(Width100%, Screen.PixelsPerInch, 96);
Where Width100% is the default value at 96 DPI. So one possible solution is to always store the width in 96 DPI equivalent, then scale it whenever is needed.

lainz

  • Hero Member
  • *****
  • Posts: 2774
  • I'm coding :)
    • Lainz
Re: Saving Window position and High DPI
« Reply #2 on: May 02, 2018, 08:18:41 pm »
Thanks. Yes, I need to change the automatic saving and try with your code.
The problem of the automatic saving / loading, is that I can't change nothing.

GetMem

  • Hero Member
  • *****
  • Posts: 3083
Re: Saving Window position and High DPI
« Reply #3 on: May 02, 2018, 08:27:44 pm »
Plese try something like this:

1. Save
Width96 := MulDiv(CurWidth, 96,  Screen.PixelsPerInch);
Save Width96 to json.

2. Load
Load Width96 from JSON:
CurWidth  := MulDiv(Width96,  Screen.PixelsPerInch, 96);

« Last Edit: May 02, 2018, 08:32:27 pm by GetMem »

lainz

  • Hero Member
  • *****
  • Posts: 2774
  • I'm coding :)
    • Lainz
Re: Saving Window position and High DPI
« Reply #4 on: May 02, 2018, 09:42:49 pm »
Thanks GetMem, it works.

Code: Pascal  [Select]
  1. procedure TfrmTimeEntry.JSONPropStorage1RestoreProperties(Sender: TObject);
  2. begin
  3.   Width := MulDiv(Width, 96, Screen.PixelsPerInch);
  4.   Height := MulDiv(Height, 96, Screen.PixelsPerInch);    
  5. end;

It's only needed on restore.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus