evilone
evilone

Reputation: 22740

FireMonkey and showing modal dialog center of the owner form

I have a problem with displaying modal dialog in center of the owner form. My code for showing modal dialog is:

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;

Tried to change Position property in designer too, but it doesn't seems to center the dialog.

Can you tell me what's wrong here?

Upvotes: 7

Views: 3695

Answers (1)

Arjen van der Spek
Arjen van der Spek

Reputation: 2660

Position is not implemented in FireMonkey by ShowModal. With the class helper below you can use: sdSettingsDialog.UpdateFormPosition before you call 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;

Upvotes: 8

Related Questions