屏幕方向 iOS

Screen orientation on iOS

根据 Delphi 中的 this question,可以使用如下代码有选择地强制 FMX 应用程序横向或纵向:

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;

我不知道如何将上面的代码转换为 C++Builder。我尝试了以下基于 on this post 的代码,但它在 iOS 和 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);
 }
}

这甚至可以在 FMX 中使用 C++Builder 实现吗?

这一行:

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

应该是这样的:

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

注意在最后一个参数中添加了 & 运算符。这甚至在 documentation:

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

此外,Intf 确实应该声明为匹配您请求的接口,例如:

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