Search code examples
delphifiremonkeyc++builder

How do I get the screen scaling in FireMonkey?


Using C++ Builder v. 10.2.3, I want to center a TForm that is being resized. To do this, I use the TScreen::WorkArea values. However, running on a system with Windows 10 scaling at 125%, the code doesn't work properly, as the TForm is scaled up. How can I determine if such scaling is occurring, and then adjust accordingly? Is there built in functionality in FireMonkey to do this? I should note that with scaling > 100% in Windows 10, the TForm::TPosition values don't seem to work properly - for example, setting it to ScreenCenter seems to have a similar problem, where it isn't actually centered.


Solution

  • This function will return the screen scaling, i.e. 1.25 if you are using 125% scaling on Windows:

    function GetScreenScale: Single;
    var ScreenService: IFMXScreenService;
    begin
      Result := 1;
      if TPlatformServices.Current.SupportsPlatformService (IFMXScreenService, IInterface(ScreenService)) then
        Result := ScreenService.GetScreenScale;
    end;
    

    Note that on Mac, the only screen scaling possible is 1.0 (non-retina) and 2.0 (retina). However, on the Mac the values returned by TScreen are already scaled, so no correction is needed.