【问题标题】:Application z order issue应用程序 z 订单问题
【发布时间】:2017-02-15 21:38:21
【问题描述】:

这可能不是我的应用程序 (XE7) 的问题,因为它只发生在我的 1 个客户工作站上,但是我想确认我正在正确编码并寻求解决它的任何指导

发生的情况是,从应用程序主窗体可能会启动一个非模态窗体,然后当其中最后一个随后关闭时,将应用程序主窗体置于 Windows Z 顺序的后面

这是我的主应用程序的启动代码

Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TDM, DM);
Application.CreateForm(TfmMain, fmMain);
Application.CreateForm(TfmReportDefn, fmReportDefn);
Application.Run;

这是启动非模态表单的代码示例

  if (fmCustomer = nil) then
     fmCustomer := TfmCustomer.Create(nil);
  else
    fmCustomer.SetFocus;

这里是非模态表单的 onclose 事件代码

procedure TfmCustomer.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  action := cafree;
  fmCustomer := nil;
end;

感谢任何回复

【问题讨论】:

    标签: delphi vcl


    【解决方案1】:

    这是启动非模态表单的代码示例

    if (fmCustomer = nil) then
      fmCustomer := TfmCustomer.Create(nil);
    else
      fmCustomer.SetFocus;
    

    我会建议更像这样的东西:

    if (fmCustomer = nil) then
      fmCustomer := TfmCustomer.Create(Application);
    fmCustomer.Show;
    

    首先,分配所有者可确保在应用关闭且用户尚未关闭表单时仍自动释放表单。

    其次,使用Show() 而不是SetFocus() 以确保表单实际可见并被带到前台。

    这里是非模态表单的 onclose 事件代码

    procedure TfmCustomer.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      action := cafree;
      fmCustomer := nil;
    end;
    

    使用caFree 很好,但我建议将nil 分配移到OnDestroy 事件中:

    procedure TfmCustomer.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Action := caFree;
    end;
    
    procedure TfmCustomer.FormDestroy(Sender: TObject);
    begin
      fmCustomer := nil;
    end;
    

    OnClose 事件只会在用户手动关闭窗口、操作系统正在关闭或您的代码调用fmCustomer.Close() 时触发。如果表单因其他任何原因被释放,您仍然需要 nil 指针,但不会调用 OnClose

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多