gpt4 book ai didi

delphi - 当所有者是 MDI Child 时,如何定位 TForm poOwnerFormCenter?

转载 作者:行者123 更新时间:2023-12-02 13:23:16 28 4
gpt4 key购买 nike

我想显示TCustomForm后代作为对话框,以便它们位于 poOwnerFormCenter 。但是当 FormStyle 时正确定位表单的相同代码是 fsNormal FormStyle 时未设置正确的位置是 fsMDIChild

当辅助形式有 FormStyle = fsNormal 时Button1 打开模式对话框,如下所示: fsNormal

但是当辅助形式有FormStyle = fsMDIChild时定位似乎是相对于 MDI 子级相对于 MDI 父级的位置,而不是 MDI 子级的绝对位置: fsMDIChild

我不确定我是否犯了任何错误,这可能是一个错误还是正常行为。

以下代码用于显示对话框:

procedure TForm3.Button1Click(Sender: TObject);
var
AModalForm: TForm;
begin
AModalForm := TForm.Create(Self);
try
AModalForm.Position := poOwnerFormCenter;
AModalForm.ShowModal;
finally
AModalForm.Free;
end;
end;

要重现的项目:

dpr

program Project2;

uses
Vcl.Forms,
Unit2 in 'Unit2.pas' {Form2},
Unit3 in 'Unit3.pas' {Form3};

{$R *.res}

var
AMainForm: TForm2;
A: TApplication;
begin
A := Application;
A.Initialize;
A.MainFormOnTaskbar := True;
A.CreateForm(TForm2, AMainForm);
A.Run;
end.

单元2

帕斯

unit Unit2;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Menus;

type
TForm2 = class(TForm)
Button1: TButton;
Panel1: TPanel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

implementation

{$R *.dfm}

uses
Unit3;

procedure TForm2.Button1Click(Sender: TObject);
var
AForm: TForm3;
begin
AForm := TForm3.Create(Self);
AForm.FormStyle := fsMDIChild;
end;

procedure TForm2.Button2Click(Sender: TObject);
var
AForm: TForm3;
begin
AForm := TForm3.Create(Self);
AForm.FormStyle := fsNormal;
end;

end.

dfm

object Form2: TForm2
Left = 0
Top = 0
Caption = 'Form2'
ClientHeight = 356
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
FormStyle = fsMDIForm
OldCreateOrder = False
Visible = True
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 97
Height = 356
Align = alLeft
TabOrder = 0
object Button1: TButton
Left = 8
Top = 39
Width = 75
Height = 25
Caption = 'fsMDIChild'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 8
Top = 8
Width = 75
Height = 25
Caption = 'fsNormal'
TabOrder = 1
OnClick = Button2Click
end
end
end

单元3

帕斯

unit Unit3;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

implementation

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
var
AModalForm: TForm;
begin
AModalForm := TForm.Create(Self);
try
AModalForm.Position := poOwnerFormCenter;
AModalForm.ShowModal;
finally
AModalForm.Free;
end;
end;


end.

dfm

object Form3: TForm3
Left = 0
Top = 0
Caption = 'Form3'
ClientHeight = 336
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Visible = True
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 8
Top = 8
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
end

最佳答案

在尝试了在 SO 和其他地方找到的几种想法之后,我所有的尝试都失败了,我想最安全的(=在所有 Delphi 版本中工作)解决方案如下。

Unit1 - Form1:TForm1 - fsMDIForm
Unit2 - Form2:TForm2 - fsMDIChild
Unit3 - AModalForm:TFom3 - ordinary form, shown modally, centered on the Form2

基本部分只是手动计算和设置 AModalFormLeftTop 属性的后备,使其居中。它还需要将 Position 属性设置为 poDesigned

// Showing the modal form centered by a fsMDIChild form
procedure TForm2.Button1Click(Sender: TObject);
var
AModalForm: TForm3;
begin
AModalForm := TForm3.Create(self);
try
AModalForm.Left := Self.ClientOrigin.X + (Self.ClientWidth-AModalForm.Width) div 2;
AModalForm.Top := Self.ClientOrigin.Y + (Self.ClientHeight-AModalForm.Height) div 2;
AModalForm.Position := poDesigned;

AModalForm.ShowModal;
// use modalresult as needed
finally
AModalForm.Free;
end;
end;

关于delphi - 当所有者是 MDI Child 时,如何定位 TForm poOwnerFormCenter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50744920/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com