0

根据Delphi 中的这个问题,可以使用如下代码选择性地将 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。我根据这篇文章尝试了以下代码,但它在 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);
 }
}

这在使用 C++Builder 的 FMX 中是否可行?

4

1 回答 1

1

这一行:

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

应该是这样的:

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

请注意&在最后一个参数中添加了运算符。这甚至在文档中都有说明:

注意:请考虑您需要在 Intf 之前添加 &,如您在上面的代码示例中所见。

此外,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);
    }
}
于 2019-10-14T18:19:15.500 回答