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 ?
Try this :
var
ScreenSize: TSize;
begin
ScreenSize := Screen.Size;
Caption := IntToStr(ScreenSize.Width) + '*' + IntToStr(ScreenSize.Height);
end;
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With