Selective disabling of LCL Scaling lazarus / 31-LCL ---------------------------------------------------------------------- simsee · 2023-10-31 15:32 My question arises, in part, from another thread I started (https://forum.lazarus.freepascal.org/index.php/topic,64696.0.html). In an application I use a TScrollBox with dynamically created TGraphicControl instances inside. I need the coordinates (Left,Top) of these objects not to change when the PPI changes. In other words, I should disable LCLScaling. I know you can do this globally at the application level with TApplication.Scaled, but I want to disable automatic scaling only for specific controls, not for all those in the application. It's possible? Thank you. ---------------------------------------------------------------------- wp · 2023-10-31 16:23 Quote from: simsee on October 31, 2023, 04:32:06 pmIn an application I use a TScrollBox with dynamically created TGraphicControl instances inside. I need the coordinates (Left,Top) of these objects not to change when the PPI changes. In other words, I should disable LCLScaling. I don't understand: When you create a control a runtime and you place it at Left = 100, it will always be at Left =100 no matter what the current resolution is. LCLScaling is executed only when a form is loaded from lfm, or when the resolution of the monitor changes at runtime (in some modes), and only for those controls which handle the resolution change in their DoAdjustLayout method. To my knowledge it is not possible to exclude some specific controls from LCLScaling. At form level, however, this is possible by switching the Scaled property of the form to false. Even if it were possible to exclude controls from LCLScaling there is the issue that these controls would become super-small when your application runs on a super-high-res monitor... Are you sure that your scrollbox handles LCL scaling for its child controls correctly? What do you want to achieve? Here is an excerpt from the wiki page on high-dpi (https://wiki.lazaru s.freepascal.org/High_DPI#High_DPI_in_Lazarus_1.8_and_above) QuoteIf you create controls run-time, scale all coordinates, sizes etc that have to be DPI-aware with TControl.Scale96ToForm() or ScaleDesignToForm() (depending on your choice of default PPI) or prepare your container (e.g. panel with controls) as it was with 96 PPI and then call TControl.AutoAdjustLayout(lapAutoAdjustForDPI, 96, ParentFormOfTheContainer.PixelsPerInch, 0, 0); ---------------------------------------------------------------------- jamie · 2023-10-31 22:32 its possible that his control has been installed and he just does a drop on it. in the control class override the "ChangeScale" procedure. each control living on a form will receive that call while scaling is taking place.  when you override it,  you can either ignore the inherited or just pass it two equal values do it does nothing . Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls; type TShape = Class(ExtCtrls.TShape)   Procedure ChangeScale(A,B:Integer); Override; end;   { TForm1 }   TForm1 = class(TForm)    Shape1: TShape;    procedure Shape1MouseUp(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Shape1MouseUp(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  ScaleBy(96,98);end; procedure TShape.ChangeScale(A,B:Integer);Begin  A:=96; B:=96;  Inherited;  Beep;end; end. ---------------------------------------------------------------------- simsee · 2023-11-01 11:26 Thank you for the precious information, on the basis of which I am making some reflections. Quote from: wp on October 31, 2023, 05:23:11 pmI don't understand: When you create a control a runtime and you place it at Left = 100, it will always be at Left =100 no matter what the current resolution is. Consider the attached project (coming from my other thread). DesignTimePPI=96 -> Left=300 Top=300 DesignTimePPI=144 -> Left=200 Top=200 As a consequence, in the first case the rectangle line is not displayed. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type   TGraph=TScrollBox;   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private    Graph : TGraph;  public   end;   { TGraphObject }   TGraphObject=class(TGraphicControl)    private      fGraphOwner : TGraph;    public      Pt1, Pt2 : TPoint;      constructor Create(Graph : TGraph; Rect : TRect); virtual; overload;      constructor Create(Graph : TGraph; X1,Y1,X2,Y2 : integer); virtual; overload;  end;   { TLine }   TLine=class(TGraphObject)    protected    procedure Paint; override;    public      constructor Create(Graph: TGraph; Rect: TRect); override; overload;      constructor Create(Graph : TGraph; X1,Y1,X2,Y2 : integer); override; overload;  end;  var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  Graph:=TGraph.Create(Self);  Graph.Parent:=Self;  Graph.Align:=alClient;  TLine.Create(Graph,Rect(200,200,300,400));end; { TGraphObject } constructor TGraphObject.Create(Graph: TGraph; Rect: TRect);begin  inherited Create(Graph);  fGraphOwner:=Graph;  Rect.NormalizeRect;  with Rect do    SetBounds(Left,Top,Width,Height);  Parent:=Graph;end; constructor TGraphObject.Create(Graph: TGraph; X1, Y1, X2, Y2: integer);begin  Create(Graph,Rect(X1,Y1,X2,Y2));end; { TLine } procedure TLine.Paint;begin  inherited;  writeln(Left,' ',Top);  Canvas.Line(Pt1.X-Left,Pt1.Y-Top,Pt2.X-Left,Pt2.Y- Top);end; constructor TLine.Create(Graph: TGraph; Rect: TRect);begin  inherited Create(Graph, Rect);  Pt1:=Rect.TopLeft;  Pt2:=Rect.BottomRight;end; constructor TLine.Create(Graph: TGraph; X1, Y1, X2, Y2: integer);begin  Create(Graph,Rect(X1,Y1,X2,Y2));end; end. ---------------------------------------------------------------------- jamie · 2023-11-01 20:49 @simsee Look, I am not going to write it for you, I show you an example of using a Tshape to intercept the changescale process.  You need to actually look at it and study it, it's simple, then when you figure it out, apply the same principle to your control. It's as simple as that. ---------------------------------------------------------------------- simsee · 2023-11-01 22:25 I'm not lazy.  Yes, it's simple. But overriding ChangeScale doesn't solve the problem. When PPI=96 the line is not shown. Here is the code. Complete project attached. Thanks for your help. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type   TGraph=TScrollBox;   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private    Graph : TGraph;  public   end;   { TGraphicControl }   TGraphicControl=class(Controls.TGraphicControl)    protected      procedure ChangeScale(Multiplier: Integer; Divider: Integer); override;  end;   { TGraphObject }   TGraphObject=class(TGraphicControl)    private      fGraphOwner : TGraph;    public      Pt1, Pt2 : TPoint;      constructor Create(Graph : TGraph; Rect : TRect); virtual; overload;      constructor Create(Graph : TGraph; X1,Y1,X2,Y2 : integer); virtual; overload;  end;   { TLine }   TLine=class(TGraphObject)    protected    procedure Paint; override;    public      constructor Create(Graph: TGraph; Rect: TRect); override; overload;      constructor Create(Graph : TGraph; X1,Y1,X2,Y2 : integer); override; overload;  end;  var  Form1: TForm1; implementation {$R *.lfm} { TGraphicControl } procedure TGraphicControl.ChangeScale(Multiplier: Integer; Divider: Integer);begin  //inherited ChangeScale(Multiplier, Divider);end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  Graph:=TGraph.Create(Self);  Graph.Parent:=Self;  Graph.Align:=alClient;  TLine.Create(Graph,Rect(200,200,300,400));end; { TGraphObject } constructor TGraphObject.Create(Graph: TGraph; Rect: TRect);begin  inherited Create(Graph);  fGraphOwner:=Graph;  Rect.NormalizeRect;  with Rect do    SetBounds(Left,Top,Width,Height);  Parent:=Graph;end; constructor TGraphObject.Create(Graph: TGraph; X1, Y1, X2, Y2: integer);begin  Create(Graph,Rect(X1,Y1,X2,Y2));end; { TLine } procedure TLine.Paint;begin  inherited;  writeln(Left,' ',Top);  Canvas.Line(Pt1.X-Left,Pt1.Y-Top,Pt2.X-Left,Pt2.Y- Top);end; constructor TLine.Create(Graph: TGraph; Rect: TRect);begin  inherited Create(Graph, Rect);  Pt1:=Rect.TopLeft;  Pt2:=Rect.BottomRight;end; constructor TLine.Create(Graph: TGraph; X1, Y1, X2, Y2: integer);begin  Create(Graph,Rect(X1,Y1,X2,Y2));end; end. EDIT: Now I don't have here the two machines with different screen resolutions. In the tests I edit the lfm file by changing the DesignTimePPI value. ---------------------------------------------------------------------- wp · 2023-11-01 22:44 The component in the "trial" project is not high-dpi aware. Every high-dpi aware component must override at least the DoAutoAdjustLayout method and scale its internal coordinates with the factors passed to the method, in your case Pt1 and Pt2: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TGraphObject=class(TGraphicControl)    ...    protected      procedure DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy;        const AXProportion, AYProportion: Double); override;    ...  end;  procedure TGraphObject.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy;  const AXProportion, AYProportion: Double);begin  inherited;  if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then  begin    Pt1.X := round(Pt1.X * AXProportion);    Pt1.Y := round(Pt1.Y * AYProportion);    Pt2.X := round(Pt2.X * AXProportion);    Pt2.Y := round(Pt2.Y * AYProportion);  end;end; If your component contains also a font which is not named "Font" you must also override the methods FixDesignFontsPPI(const ADesignTimePPI: Integer) and ScaleFontsPPI(const AToPPI: Integer; const AProportion: Double)  (look at the "grids" sources for an example). [PS] I am afraid that part of what I wrote in reply #1 is not correct. ---------------------------------------------------------------------- simsee · 2023-11-01 23:18 @WP: With your solution the line is shown with PPI=96 and PPI=144. Wanting to preserve the Left and Top values of the TGraphObject instances, I made this change: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TGraphObject.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy;  const AXProportion, AYProportion: Double);begin  inherited;  if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then  begin    Left := round(Left / AXProportion);    Top := round(Top / AYProportion);  end;end; Now the problem seems solved. But I need to delve deeper. Thanks for now! ---------------------------------------------------------------------- simsee · 2023-11-02 10:02 Quote from: wp on November 01, 2023, 11:44:04 pm[PS] I am afraid that part of what I wrote in reply #1 is not correct. No problem. Your contribution was notable (as always, after all). My problem of wanting to avoid scaling the Left and Top parameters remains open. Setting Scaled:=False in the form containing the ScrollBox solves the problem. But I would like to keep the scaling on the form and prevent it only in the ScrollBox. I tried embedding the Scrollbox inside a TCustomDesignControl instanced, so I could set the Scaled property, but it didn't work. For this reason I also looked with great interest at Jamie's solution, but unfortunately in my case did not work. ---------------------------------------------------------------------- wp · 2023-11-02 17:12 Quote from: simsee on November 02, 2023, 11:02:16 amMy problem of wanting to avoid scaling the Left and Top parameters remains open. But in the previous post you wrote that "now the problem seems solved". I am confused... Yes, you can undo the effect of scaling by dividing the coordinate/length/width/height/distance or whatever by the scale factor. But you should also handle the Width and Height: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TGraphObject.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy;  const AXProportion, AYProportion: Double);begin  inherited;  if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then  begin    Left := round(Left / AXProportion);    Top := round(Top / AYProportion);    Width := round(Width / AXProportion);    Height := round(Height / AYProportion);  end;end; Of course, this looks a bit over-complicated: first the control is scaled and multiplied by the proportions, and later it is unscaled by dividing by the proportions... Maybe it would be better to subclass the scrollbox such that it does not scale its child controls? But then, if you add "normal" controls to this specific scrollbox, they will not be scaled although they probably should. I still do not understand what this is all about. Why can't you scale the control as usual, like I showed in my code in the other post? Is the problem that your sample code should draw a line between (200,200) and (300,400), but it actually draws it between (300,300) and (450, 600)? ---------------------------------------------------------------------- simsee · 2023-11-02 18:45 I have summarized a somewhat complex context. I'll try to explain myself better, remaining concise. I wrote a component (derived from TScrollBox) that allows you to create flowcharts. Allows you to draw nodes (rectangles, ellipses), which can be joined using arrows. These objects (which are TGraphicControl) can be moved and resized, maintaining connections. The component also supports load/save and undo/redo functions. In practice it similar to the EvsSimpleGraph component available in OPM, but it's more basic. I could use EvsSimpleGraph, but I wanted to make something myself, for fun and learning. The component works well at any resolution when I place objects with the mouse. The problem is when I use a constructor to insert objects. With different PPIs I have coordinate scaling. I want to keep the original (=absolute) coordinates of the objects specified as parameters in constructor, otherwise I have inconsistencies and consequent display problems. Exactly what you guessed:Quote from: wp on November 02, 2023, 06:12:46 pm Maybe it would be better to subclass the scrollbox such that it does not scale its child controls? But then, if you add "normal" controls to this specific scrollbox, they will not be scaled although they probably should. I still do not understand what this is all about. Why can't you scale the control as usual, like I showed in my code in the other post? Is the problem that your sample code should draw a line between (200,200) and (300,400), but it actually draws it between (300,300) and (450, 600)? How can I prevent a control that is a subclass of TScrollBox from scaling its coordinates with different DesignTimePPI? ---------------------------------------------------------------------- wp · 2023-11-02 22:32 Here is another idea that I had after tracing the LCL code with the debugger: Run-time created controls are scaled in the AfterConstruction method of the form. This occurs AFTER its OnCreate event. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // from "Forms"procedure TCustomForm.AfterConstruction;var  MonPPI: Integer;begin  SetRestoredBounds(Left, Top, Width, Height, True);  DoCreate;                         // <---- OnCreate fires here.  EndFormUpdate;  inherited AfterConstruction;   MonPPI := Monitor.PixelsPerInch;  if Application.Scaled and Scaled and (MonPPI > 0) and (MonPPI <> PixelsPerInch)  and not (csDesigning in ComponentState) then    AutoAdjustLayout(lapAutoAdjustForDPI, PixelsPerInch, MonPPI, Width, MulDiv(Width, MonPPI, PixelsPerInch));  // <--- scaling occurs hereend; So, when you create your custom controls in the form's OnCreate event, their coordinates will be scaled - that's what you don't want to happen. But you can force scaling already in the onCreate event by calling the AutoAdjustLayout method of the form by yourself. When you then create your controls afterwards the given coordinates refer to the new resolution at which the form runs. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);begin  Graph:=TGraph.Create(Self);  Graph.Parent:=Self;  Graph.Align:=alClient;  AutoAdjustLayout(lapAutoAdjustForDPI, 96, Screen.PixelsPerInch, 0, 0);   // <---- ADDED  TLine.Create(Graph,Rect(200,200,300,400));end;If you wonder whether the controls won't be scaled a second time in the AfterConstruction method: no - because the form's PixelsPerInch then already are at the monitor's PPI, and the AutoAdjustLayout in AfterConstruction call will simply be bypassed. Still, there may be a problem, namely when the PixelsPerInch change during the runtime of the application, for example when the user drags the form on a monitor with a different resolution. When this happens, AutoAdjustLayout will be called again, and all controls will shrink or expand corresponding to the new resolution, and the coordinates of the TLine in above example could change to, e.g.,  133, 133, 200, 266. The internal coordinates P1 and P2 of your component must follow this change, otherwise the line drawn between these two points will not be correct any more. For this reason you must override the DoAutoAdjustLayout method again, but now with the multiplication: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TGraphObject.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy;  const AXProportion, AYProportion: Double);begin  inherited;  if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then  begin    Pt1.X := round(Pt1.X * AXProportion);    Pt1.Y := round(Pt1.Y * AYProportion);    Pt2.X := round(Pt2.X * AXProportion);    Pt2.Y := round(Pt2.Y * AYProportion);  end;end; ---------------------------------------------------------------------- simsee · 2023-11-03 11:29 Dear Wp, thank you for the time you are dedicating to me. Your solution seems to solve the problem I highlighted. I think the only drawback is the fact that it disables scaling on the entire form that contains the control derived from TScrollBox. So if I have other controls created at run time in the form that need to be scaled (buttons, menus, etc), the scaling mechanism will not act on them. For controls created on the form at design time, it seems to me that the scaling is maintained. Is my interpretation correct?