在firemonkey

时间:2015-06-30 15:21:53

标签: forms delphi firemonkey delphi-xe7

在firemonkey中显示和关闭表单时,应用程序无法记住最后一个激活的表单,并激活了错误的表单。

如何激活最后一个活动表单而不是应用程序选择的任意表单?

要复制:创建3个表单并从之前的表单中连续打开每个表单。

我是一个mainform和2个ChildForms,第二个表单是第三个表单的父级。

我从我的MainForm打开第一个childForm。

var
  tmpForm2:TForm2;
begin
  tmpForm2:=TForm2.Create(self);
  tmpForm2.Show;
end;

在此表单中,有一个显示第二个子表单的按钮

var
  form3:Tform3;
begin
  form3:=TForm3.Create(nil);
  form3.Show;
end;

当我打开第二个ChildForm并关闭它时, Mainform已激活。而不是第一个ChildForm

现在我重复这个过程,但是当关闭第二个ChildForm时,第一个ChildForm会被激活,正如人们所期望的那样。

下次重新激活Mainform时,顺序会保持chainging,而不是真正的最后一个活动表单。

2 个答案:

答案 0 :(得分:2)

在功能

中看起来是Delphi XE7 / XE7 Update 1中的错误
  

function TScreen.NextActiveForm(const OldActiveForm:   TCommonCustomForm):TCommonCustomForm;

在Delphi XE8上,此功能正常工作,您将返回上一个窗口。

在XE8中,他们重写函数函数TScreen.NextActiveForm(const OldActiveForm:TCommonCustomForm):TCommonCustomForm;

XE7的狗钉。我从XE8复制函数并在关闭表单之前使用它。 我只在Windows平台上测试过它。

unit ufmForm3;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;

type
  TfmForm3 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    function NextActiveForm(const OldActiveForm: TCommonCustomForm): TCommonCustomForm;
  end;

var
  fmForm3: TfmForm3;

implementation

{$R *.fmx}

procedure TfmForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  NextActiveForm(Self);
end;

function TfmForm3.NextActiveForm(const OldActiveForm: TCommonCustomForm): TCommonCustomForm;
var
  I, CurrIndex: integer;
begin
  Result := nil;
  CurrIndex := Screen.IndexFormOfObject(OldActiveForm);
  if CurrIndex >= 0 then
  begin
    I := CurrIndex - 1;
    while (I >= 0) and (Screen.Forms[I].Released or not Screen.Forms[I].Visible) do
      Dec(I);
    if I < 0 then
    begin
      I := Screen.FormCount - 1;
      while (I >= 0) and (I <> CurrIndex) and (Screen.Forms[I].Released or not Screen.Forms[I].Visible) do
        Dec(I);
    end;
    if (I >= 0) and (I <> CurrIndex) then
    begin
      Result := Screen.Forms[I];
      Screen.ActiveForm := Result;
    end;
  end;
end;


end.

答案 1 :(得分:0)

我在Delphi FMX Berlin遇到了相关问题。我的SDI应用程序有一个隐藏的“真实”主表单和一个或多个工作表单实例。当其中一个工作形式称为模态对话框时,我发现在关闭对话框时,焦点将转移到与调用形式不同的形式。解决方案结果很简单。

(1)使用所有者Self:

创建对话框
MyDlg := TMyDlg.Create(Self);
MyDlg.ShowModal;

(2)在模式对话框OnClose中使用以下内容:

procedure TMyDlg.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Action := TCloseAction.caFree;
    Screen.ActiveForm:=TMySDIAppForm(Self.Owner);
end;