delphi - 使用 CreateParam 的 Params.WndParent 强制创建子窗体?

标签 delphi

在我继承并迁移到 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/

相关文章:

Delphi如何在TCanvas上快速绘制TColor的二维数组?

delphi - 如何在Delphi中为伪浏览器启用cookie?

delphi - 为什么我在 Delphi TMainMenu 中看不到热键下划线

delphi - delphi中初始化对象

delphi - 自上而下迭代通用集合(TDictionary)

delphi - 在delphi中使用lib文件。如何导入lib文件

delphi - TPNGImage `LoadFromStream` 在某些情况下不工作

delphi - 如何更改 Delphi 中的事件构建配置?

android - 如何获取内置传感器的名称、制造商和序列号

Delphi:TRichEdit 非默认非 Unicode 系统语言中的文本作为字符串 (ANSI)