7

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

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 属性,但它似乎没有使对话框居中。

你能告诉我这里有什么问题吗?

4

1 回答 1

8

ShowModal 在 FireMonkey 中没有实现位置。使用下面的类助手,您可以在调用 ShowModal 之前使用:sdSettingsDialog.UpdateFormPosition:

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;
于 2011-11-19T16:39:55.833 回答