我在所有者表单的中心显示模式对话框时遇到问题。我显示模态对话框的代码是:
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属性,但它似乎并不以对话框为中心。
你能告诉我这里有什么问题吗?
答案 0 :(得分: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;