Home > Net >  Scaling of coordinates between different screen resolutions
Scaling of coordinates between different screen resolutions

Time:07-16

I have projects developed in Delphi 10 on a laptop with a screen resolution of 96. I am now using Delphi 10.4 Community Edition on a Microsoft Surface with a screen resolution of 201. Is there a function or a settings that automatically converts numerically defined coordinates when scaling an application? To show what I mean I add this code snippet.

procedure TForm1.Button1Click(Sender: TObject);
begin
  with Canvas do
  begin
    MoveTo(0,0);
    LineTo(400,250);
    MoveTo(0,0);
    LineTo(ClientWidth,ClientHeight);
  end;
end;

The first line drawn does not scale, the second one obviously does.

May I add:

If I compile the same code on my old laptop with a screen resolution of 96 and then run the exe file on my Surface Laptop with a screen resolution of 201 it scales ok, I was hoping there was a facility somewhere to compile my old programmes on my new computer without having to manually change all the code referring to coordinates x and y.

CodePudding user response:

There's no built-in scaling of coordinates in a TCanvas. You can use this CLASS HELPER:

TYPE
  TFormHelper = CLASS HELPER FOR TForm
                  FUNCTION Scale(Value : INTEGER) : INTEGER;
                END;

FUNCTION TFormHelper.Scale(Value : INTEGER) : INTEGER;
  BEGIN
    Result:=MulDiv(Value,CurrentPPI,Screen.DefaultPixelsPerInch)
  END;

Use it as in:

procedure TForm1.Button1Click(Sender: TObject);
begin
  with Canvas do
  begin
    MoveTo(Scale(0),Scale(0)); // Not needed, as 0 scaled is always 0, but... //
    LineTo(Scale(400),Scale(250));
    MoveTo(0,0);
    LineTo(ClientWidth,ClientHeight);
  end;
end;

CodePudding user response:

To run my projects developed with Delphi 10 on my new computer with a higher screen resolution, using Delphi 10.4 Community Edition I change the setting Project Options -> Application -> Manifest -> DPI Awareness to GDI Scaling and it all works like a charm.

  • Related