Bill
Bill

Reputation: 3033

How to get screen size in Firemonkey FM3?

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 ?

Upvotes: 7

Views: 12123

Answers (4)

bLight
bLight

Reputation: 865

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.

Upvotes: 3

bLight
bLight

Reputation: 865

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;

Upvotes: 2

Pavel Jiri Strnad
Pavel Jiri Strnad

Reputation: 314

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.

Upvotes: 5

S.MAHDI
S.MAHDI

Reputation: 1022

Try this :

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

Upvotes: 10

Related Questions