7

How to get screen dimensions with Firemonkey FM³ ? The following code:

var
  Size: TPointF;
begin
  Size := FMX.Platform.IFMXScreenService.GetScreenSize;
  ...
end;

Results in this compiler error:

[dcc32 Error] Unit1.pas(46): E2018 Record, object or class type required

How should I use IFMXScreenService interface to get screen size ?

TLama
  • 75,147
  • 17
  • 214
  • 392
Bill
  • 2,993
  • 5
  • 37
  • 71
  • 1
    Here is [`an example`](http://blogs.embarcadero.com/pawelglowacki/2012/10/08/39817) of proper use of the [`IFMXScreenService`](http://docwiki.embarcadero.com/Libraries/XE4/en/FMX.Platform.IFMXScreenService) interface. – TLama Oct 06 '13 at 19:52
  • 1
    @Bill You cannot call methods on an interface type. You need to obtain an interface reference before you can actually execute methods. – David Heffernan Oct 06 '13 at 22:04

4 Answers4

10

Try this :

var
  ScreenSize: TSize;
begin
  ScreenSize := Screen.Size;
  Caption := IntToStr(ScreenSize.Width) + '*' + IntToStr(ScreenSize.Height);
end;
S.MAHDI
  • 1,022
  • 8
  • 15
  • What exactly is `Screen`? – David Heffernan Oct 06 '13 at 22:03
  • Screen is a global varialble (Screen:TScreen) declared in FMX.Forms unit , probably its only exist in XE4 .. (the code was tested using DXE4). – S.MAHDI Oct 06 '13 at 22:17
  • "Screen" doesn't work under Android, for example on my Galaxy Note 4, Screen returns a resolution of 360x640 instead of the display's real resolution of 2560x1440. – bLight Dec 29 '17 at 18:09
5

It's not so simple.

Firemonkey has feature called resolution http://docwiki.embarcadero.com/RADStudio/XE5/en/Working_with_Native_and_Custom_FireMonkey_Styles

It's actualy cool feature. If you work with screens that has retina display then whatever you would paint on the screen will be really small. For example pixel resolution of iPhone is close to iPad 1 and 2, but screen is twice bigger.

So on iPhone will

var
  ScreenSize: TSize;
begin
  ScreenSize := Screen.Size;
  Caption := IntToStr(ScreenSize.Width) + '*' + IntToStr(ScreenSize.Height);
end;

will look like 320x480. And same the forms.

But if you use uses FMX.Platform;

procedure ShowScreenSize;
var
  ScreenSvc: IFMXScreenService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenSvc)) then
  begin
    ScreenSize := Format('Resolution: %fX%f', [ScreenSvc.GetScreenSize.X, ScreenSvc.GetScreenSize.Y]);
    ShowMessageFmt('Screen.Width = %g, Screen.Height = %g', [ScreenSize.X, ScreenSize.Y]);
  end;
end;

You get actual screen resolution in pixels.

This also apply to Mac with Retina display.

3

Here's a different solution that doesn't require multiplication by scale:

var
  aResolution      : JPoint;
begin
  aResolution := TJPoint.Create;
  TAndroidHelper.Display.getRealSize(aResolution);
end;

Works well in Delphi 10.3 RIO. From what I understand, "getRealSize" requires at least Android 4.2, but since Delphi RIO doesn't even support old versions of Android, I don't believe this is a show-stopper.

bLight
  • 803
  • 7
  • 23
2

Here is a slightly more complete/clear answer to get the actual screen resolution in pixels on Android (possibly iOS, didn't test) devices:

var
   clientScreenScale   : Single;
   clientScreenSize    : TSize;
   clientScreenService : IFMXScreenService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(clientScreenService)) then
  begin
    clientScreenScale   := clientScreenService.GetScreenScale;
  end
  else clientScreenScale   := 1;

  // The display device's width:
  clientScreenSize.CX   := Round(clientScreenService.GetScreenSize.X*clientScreenScale);

  // The display device's height:
  clientScreenSize.CY   := Round(clientScreenService.GetScreenSize.Y*clientScreenScale);
end;
bLight
  • 803
  • 7
  • 23