我在所有者表单的中心显示模式对话框时遇到问题。我显示模式对话框的代码是:

procedure TfrmMain.btnOpenSettingsClick(Sender: TObject);
var
  sdSettingsDialog: TdlgSettings;

begin
   sdSettingsDialog := TdlgSettings.Create(Self);
   sdSettingsDialog.Position := TFormPosition.poOwnerFormCenter;

   try
      sdSettingsDialog.ShowModal;
   finally
     sdSettingsDialog.Free;
   end;
end;


试图也更改设计器中的Position属性,但是它似乎并未使对话框居中。

你能告诉我这里怎么了吗?

最佳答案

ShowModal在FireMonkey中未实现位置。
使用下面的类帮助器,您可以使用:sdSettingsDialog.UpdateFormPosition,然后调用ShowModal:

type
  TFormHelper = class helper for TForm
    procedure UpdateFormPosition;
  end;

procedure TFormHelper.UpdateFormPosition;
var
  RefForm: TCommonCustomForm;
begin
  RefForm := nil;

  case Position of
    // TFormPosition.poScreenCenter: implemented in FMX.Forms (only one)
    TFormPosition.poOwnerFormCenter:
      if Assigned(Owner) and (Owner is TCommonCustomForm) then
        RefForm := Owner as TCommonCustomForm;
    TFormPosition.poMainFormCenter:
      RefForm := Application.MainForm;
  end;

  if Assigned(RefForm) then
  begin
    SetBounds(
      System.Round((RefForm.Width - Width) / 2) + RefForm.Left,
      System.Round((RefForm.Height - Height) / 2) + RefForm.Top,
      Width, Height);
  end;
end;

10-05 22:25