Screen orientation on iOS

435 Views Asked by At

Per this question in Delphi an FMX app can be selectively forced into landscape or portrait with code like this:

procedure TForm1.Chart1Click(Sender: TObject);
begin
  if Application.FormFactor.Orientations = [TScreenOrientation.Landscape] then
     Application.FormFactor.Orientations := [TScreenOrientation.Portrait]
  else
     Application.FormFactor.Orientations := [TScreenOrientation.Landscape];
  end;
end;

I can't figure out how to translate this code above to C++Builder. I tried the following code based on this post but it gives access violation on both iOS and Android:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 _di_IInterface Intf;
 if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService), Intf))
 {
 _di_IFMXScreenService ScreenService = Intf;
 TScreenOrientations Orientation;
 Orientation << TScreenOrientation::Landscape;
 ScreenService->SetScreenOrientation(Orientation);
 }
}

Is this even doable in FMX with C++Builder?

1

There are 1 best solutions below

3
Remy Lebeau On BEST ANSWER

This line:

if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService), Intf))

should be this instead:

if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService), &Intf))

Note the addition of the & operator in the last parameter. This is even stated in the documentation:

Note: Please consider that you need to add & before Intf, as you can see in the code sample above.

Also, Intf really should be declared to match the interface you are requesting, eg:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    _di_IFMXScreenService ScreenService;
    if (TPlatformServices::Current->SupportsPlatformService(__uuidof(IFMXScreenService), &ScreenService))
    {
        TScreenOrientations Orientation;
        Orientation << TScreenOrientation::Landscape;
        ScreenService->SetScreenOrientation(Orientation);
    }
}