在我继承并迁移到 XE5 的一些遗留 D7 代码中,我发现了下面的代码。
注释指出,如果它是从非 WinControl 创建的,它会欺骗 Windows 认为它是子窗体。代码库中有一个地方调用 Create 时 AOwner 为 nil。 (在调用时可以使用表格,因此不确定他们为什么这样做......)
对于程序员的目标有什么建议吗?
private
FParentWinControl: TWinControl; {Don't mess with! Used in CreateParams}
procedure TFormX.CreateParams(var params: TCreateParams); override;
public
constructor TFormX.Create( AOwner: TComponent); reintroduce;
end;
constructor TFormX.Create( AOwner: TComponent);
begin
if AOwner IS TWinControl then
FParentWinControl := TWinControl(AOwner)
else
FParentWinControl := NIL;
inherited Create(AOwner);
end; { Create }
procedure TFormX.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
if (NOT fCreateParamsHasBeenRun) then
begin
fCreateParamsHasBeenRun := TRUE;
if Assigned(FParentWinControl) then
Params.WndParent := FParentWinControl.Handle; {tricks windows into thinking it's a child form}
end;
end;
最佳答案
此代码早于并大致模仿 PopupMode
and PopupParent
在 Delphi 8 中添加到 TCustomForm
的属性。假设 AOwner
是另一个 Form,请在现代 Delphi 版本中使用这些属性,例如:
constructor TFormX.Create( AOwner: TComponent);
begin
inherited Create(AOwner);
if AOwner Is TCustomForm then
PopupParent := TCustomForm(AOwner);
end;
此外,fCreateParamsHasBeenRun
的使用是错误的。每次(重新)创建表单窗口时都会调用 CreateParams()
,因此每次都需要应用 WndParent
,而不是有条件地应用。如果您需要保留 CreateParams()
逻辑(例如,如果 AOwner
是非 TCustomForm
窗口控件),则需要删除 fCreateParamsHasBeenRun
。
关于delphi - 使用 CreateParam 的 Params.WndParent 强制创建子窗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087196/