Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 884
evilone Avatar asked Nov 19 '11 15:11

evilone


1 Answers

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;
like image 120
Arjen van der Spek Avatar answered Sep 21 '22 06:09

Arjen van der Spek