Recent

Author Topic: Saving Form sizes using Windows DPI aware  (Read 8668 times)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Saving Form sizes using Windows DPI aware
« on: July 23, 2019, 11:36:04 pm »
I'm trying to debug some appearance issues using the Windows DPI aware settings.

When I hide a Form I save it's width and height, as well as column widths on a DrawGrid. Each time I rerun the application, the sizes are multiplied by a scaling factor, so the Form and column widths increase each time. How can I circumvent this?

A separate question is when I design a form on a Mac and set a Control size to, say, 20, it becomes, say, 30, when I open the project in Windows. This also happens with control spacing in the Anchor Editor, changing my standard 12 to 19. I guess this is by design, but practically it means I need to do all my design on Mac, and switch back and forth when I find bugs.

I'll throw in a third question. Does anyone just not use DPI aware?

I'd appreciate any insight or suggestions.
« Last Edit: July 23, 2019, 11:47:08 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Saving Form sizes using Windows DPI aware
« Reply #1 on: July 24, 2019, 12:14:48 am »
Maybe I should be saving:

sw := Self.ScaleFormTo96(Self.Width);


EDIT: Answering my own questions. :)

Yes, then restore with:

Self.Width := Self.Scale96ToForm(sw);
« Last Edit: July 24, 2019, 01:00:38 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

wp

  • Hero Member
  • *****
  • Posts: 11908
Re: Saving Form sizes using Windows DPI aware
« Reply #2 on: July 24, 2019, 12:23:57 am »
Can you post a simple project which shows the issue?

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Saving Form sizes using Windows DPI aware
« Reply #3 on: July 24, 2019, 02:39:13 am »
Thanks wp. Probably.

I'm still trying to nail down what the behavior is on different platforms. I thought I had it figured out on Windows, but now it seems something unexpected is happening on Mac when LCL scaling is on. It looks to me like the Form size (and grid column widths) is rescaled, but not it's position. I figured neither would be. Then I'll have a look at Linux...

The funny thing is that on Mac everything is shrinking, on Windows it was growing! :)
« Last Edit: July 24, 2019, 02:46:29 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5462
  • Compiler Developer
Re: Saving Form sizes using Windows DPI aware
« Reply #4 on: July 24, 2019, 09:36:29 am »
I've reported a similar issue with the SQLDBRest demos provided with Lazarus.

wp

  • Hero Member
  • *****
  • Posts: 11908
Re: Saving Form sizes using Windows DPI aware
« Reply #5 on: July 24, 2019, 10:34:12 am »
I can't say much about Mac, I only remember a discussion on the mailing list where it was mentioned that its default resolution is 72 dpi but LCL assumes 96 dpi. This would make a factor 96/72 = 1.33. But the discrepancy that you observe is larger, isn't it?

But without seeing what you are doing exactly it is not possible to tell whether this really is an issue of LCL scaling.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Saving Form sizes using Windows DPI aware
« Reply #6 on: July 24, 2019, 05:07:03 pm »
I think I figured it out this morning, it was making me crazy. The weird behavior started after I switched on LCL scaling, everything worked fine before.

I have methods to set and get preferences, which include the Form position, size, column widths, etc.

I was calling GetPrefs in FormHide, which saved the settings perfectly in display coordinates. But I was calling SetPrefs in FormCreate, this read the preferences fine, but interpreted them as design coordinates, which were rescaled before showing the form. %)

The solution, probably obvious in retrospect, was to move SetPrefs from FormCreate to FormShow, after the Form has been rescaled to display coordinates. It seems to be working.

I think you (wp) may be right about the 96 vs 72 on the Mac, and maybe that is why the (shrinking) scaling was occurring. However, the form design scaling is the same on Mac and Linux (e.g., anchor spacings are the same), but is rescaled on Windows. Actually, I may have found an answer to that (my second question). There is a "Force DPI scaling in design time" option, I'll try turning that off to see if I can design in 96 DPI on all platforms.


EDIT: This means I do not need to call:

sw := Self.ScaleFormTo96(Self.Width);
Self.Width := Self.Scale96ToForm(sw);

that was a red herring.
« Last Edit: July 24, 2019, 05:27:15 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Saving Form sizes using Windows DPI aware
« Reply #7 on: July 24, 2019, 05:15:11 pm »
I've reported a similar issue with the SQLDBRest demos provided with Lazarus.

Is it possible to check if they are setting form size in FormCreate?
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Saving Form sizes using Windows DPI aware
« Reply #8 on: July 24, 2019, 05:54:52 pm »
Yes, they do. If you follow the Tform.create you'll reach the class TCustomDesignControl:

Code: Pascal  [Select][+][-]
  1.  
  2. constructor TCustomDesignControl.Create(TheOwner: TComponent);
  3. begin
  4.   inherited Create(TheOwner);
  5.  
  6.   FScaled := True;
  7.   FDesignTimePPI := 96;
  8.   FPixelsPerInch := FDesignTimePPI;
  9.   if Application.Scaled then
  10.     Font.PixelsPerInch := FDesignTimePPI;
  11.  
  12. end;
  13.  

There are always problems with DPI aware.

Winni

wp

  • Hero Member
  • *****
  • Posts: 11908
Re: Saving Form sizes using Windows DPI aware
« Reply #9 on: July 24, 2019, 06:21:11 pm »
There are always problems with DPI aware.
Unfortunately, many third-party components were written long before Lazarus came up with LCL scaling, and since many of them are no longer maintened there is almost no chance that this will change. But don't blame LCL scaling for this.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Saving Form sizes using Windows DPI aware
« Reply #10 on: July 24, 2019, 08:55:31 pm »
I spoke too soon. Everything works fine on Mac and Linux, but on Windows the problem remains. :(

Turning off the "Force DPI scaling in design time" option did not seem to do anything, so I am leaving it on.

No luck isolating it yet.
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Saving Form sizes using Windows DPI aware
« Reply #11 on: July 24, 2019, 09:05:19 pm »
Not quiete shure but what about this in the lpr file

Code: Pascal  [Select][+][-]
  1. application.scaled := false
  2.  

Should turn of every scaling in the app

Winni

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Saving Form sizes using Windows DPI aware
« Reply #12 on: July 24, 2019, 10:41:29 pm »
Not quiete shure but what about this in the lpr file

Code: Pascal  [Select][+][-]
  1. application.scaled := false
  2.  

Should turn of every scaling in the app

Winni

Wow, thanks! It was driving me nuts.

Turning off LCL scaling did not work. Turning on LCL scaling seems to have set it true, but turning it off did not set it back to false.

I still am getting rescaling in the Form Editor on Win 7, which does not occur on Mac, Linux, or Win32 on XT, even though I set forced DPI scaling off.

« Last Edit: July 24, 2019, 11:03:44 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Saving Form sizes using Windows DPI aware
« Reply #13 on: July 24, 2019, 11:06:54 pm »
I got it running in Windows, but now reopening the project on Mac, all the sizes are 1.5 times too big, including all anchor spacings!! Arrrgh!  :(

How do I stop Forms being rescaled on Windows?

EDIT: I restored the Form to it's design scaling by turning back on the "Force DPI scaling in design time", and saving it on Windows. But that was a bit of a scare, and I'd really like to see the same design coordinates on all platforms.
« Last Edit: July 24, 2019, 11:23:28 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Saving Form sizes using Windows DPI aware
« Reply #14 on: July 24, 2019, 11:09:11 pm »
Just a guess:

If it only happens in Win7, what about a Windows speciality: the anoying manifest files?

Are they turned of in the project options?  Or do they interact strange with the DPI stuff?

Only a guess.

Winni

 

TinyPortal © 2005-2018