Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 537
Bill Avatar asked Oct 06 '13 19:10

Bill


3 Answers

Try this :

var
  ScreenSize: TSize;
begin
  ScreenSize := Screen.Size;
  Caption := IntToStr(ScreenSize.Width) + '*' + IntToStr(ScreenSize.Height);
end;
like image 121
S.MAHDI Avatar answered Oct 04 '22 09:10

S.MAHDI


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.

like image 21
Pavel Jiri Strnad Avatar answered Oct 04 '22 09:10

Pavel Jiri Strnad


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;
like image 43
bLight Avatar answered Oct 04 '22 09:10

bLight