【问题标题】:Screen orientation on iOSiOS 上的屏幕方向
【发布时间】:2019-10-12 13:48:17
【问题描述】:

根据 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);
 }
}

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

【问题讨论】:

    标签: firemonkey c++builder c++builder-10.3-rio


    【解决方案1】:

    这一行:

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

    应该是这样的:

    if (TPlatformServices::Current-&gt;SupportsPlatformService(__uuidof(IFMXScreenService), &amp;Intf))

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

    注意:请注意您需要在 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);
        }
    }
    

    【讨论】:

    • 当,谢谢雷米。该代码现在可以工作(没有访问冲突)但没有任何反应。我在 if 块内放了一个ShowMessage,它正在触发。只是不改变屏幕方向(在 iOS 上测试)。
    • 谢谢 Remy - 你给的最后一个链接让我觉得这是我的坏方法 - 我只需要没有设置方向的能力(以不同的方式构建应用程序)。
    猜你喜欢
    • 2013-06-02
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    相关资源
    最近更新 更多