Delphi &引用;控制'';“没有父窗口”;错误

Delphi &引用;控制'';“没有父窗口”;错误,delphi,delphi-xe,Delphi,Delphi Xe,当我将控件放在窗体上时,会出现此错误。 错误出现在此处: TAssociateFileExt = class(TGroupBox) private protected public btnAssociate : TButton; constructor Create(aOwner: TComponent); override; end; constructor TAssociateFileExt.Create(aOwner: TComponent); b

当我将控件放在窗体上时,会出现此错误。 错误出现在此处:

  TAssociateFileExt = class(TGroupBox)
  private
  protected
  public
    btnAssociate   : TButton;
    constructor Create(aOwner: TComponent); override;
  end;

constructor TAssociateFileExt.Create(aOwner: TComponent);
begin
 inherited Create(aOwner);
 Caption:= '';                       <-------- error here
 ClientHeight:= 125;                 <-------- and here
 ClientWidth := 170;
 DoubleBuffered:= TRUE;

 btnAssociate:= TButton.Create(Self);
 btnAssociate.Parent:= Self;
 btnAssociate.Visible:= TRUE;
 btnAssociate.Left:= 17;
 btnAssociate.Top:= 26;
 btnAssociate.Width:= 142;
 btnAssociate.Height:= 25;
 btnAssociate.Hint:= 'Associate this application with its files. When you double click a file this program will automatically start and load that file.';
 btnAssociate.Caption:= 'Associate';
 btnAssociate.DoubleBuffered:= TRUE;
 btnAssociate.ParentDoubleBuffered:= FALSE;
 btnAssociate.TabOrder:= 0;
 btnAssociate.OnClick:= btnAssociateClick;

 btnAssociateDel:= TButton.Create(Self);
 btnAssociateDel.Parent:= Self;
 btnAssociateDel.Visible:= TRUE;
 btnAssociateDel.Left:= 17;
 btnAssociateDel.Top:= 53;
 btnAssociateDel.Width:= 142;
 btnAssociateDel.Height:= 25;
 btnAssociateDel.Hint:= 'Remove association';
 btnAssociateDel.Caption:= 'Remove association';
 btnAssociateDel.DoubleBuffered:= TRUE;
 btnAssociateDel.ParentDoubleBuffered:= FALSE;
 btnAssociateDel.TabOrder:= 1;
 btnAssociateDel.OnClick:= btnAssociateDelClick;

 chkAllUsers:= TCheckBox.Create(Self);
 chkAllUsers.Parent:= Self;
 chkAllUsers.Visible:= TRUE;
 chkAllUsers.Left:= 31;
 chkAllUsers.Top:= 97;
 chkAllUsers.Width:= 115;
 chkAllUsers.Height:= 17;
 chkAllUsers.Hint:= 'Please note that if you want to do this for all users then you need administrator/elevated rights.';
 chkAllUsers.Caption:= 'Do this for all users';
 chkAllUsers.DoubleBuffered:= TRUE;
 chkAllUsers.ParentDoubleBuffered:= FALSE;
 chkAllUsers.TabOrder:= 2;
 chkAllUsers.OnClick:= chkAllUsersClick;
end;
TAssociateFileExt=class(TGroupBox)
私有的
受保护的
公众的
b关联:t按钮;
构造函数创建(aOwner:TComponent);推翻
结束;
构造函数TAssociateFileExt.Create(aOwner:TComponent);
开始
继承的创建(AOOwner);

标题:='' 我在CreateWindowHandle中移动了代码。现在它起作用了。 完整代码:

UNIT cAssociateExt;

INTERFACE

USES
  Windows, Messages, SysUtils, Classes, Controls, ExtCtrls, Forms, StdCtrls;

TYPE
  TAssociateFileExt = class(TGroupBox)
  private
  protected
  public
    btnAssociate   : TButton;
    btnAssociateDel: TButton;
    chkAllUsers    : TCheckBox;
    constructor Create(aOwner: TComponent); override;
    procedure AfterConstruction;  override;
    procedure CreateWindowHandle(const Params: TCreateParams); override;
    ...
  published
  end;


procedure Register;


IMPLEMENTATION


procedure TAssociateFileExt.AfterConstruction;
begin
 inherited;         //Not a good place here
end;


procedure TAssociateFileExt.CreateWindowHandle(const Params: TCreateParams);
begin
 inherited;

 //DO NOT CREATE CONTROLS HERE! See: Sertac Akyuz's comment

 Caption:= '';    
 ClientHeight:= 125;
 ClientWidth := 170;
end;



constructor TAssociateFileExt.Create(aOwner: TComponent);
begin
 inherited Create(aOwner);
 DoubleBuffered:= TRUE;

 btnAssociate:= TButton.Create(Self);
 btnAssociate.Parent:= Self;
 btnAssociate.Visible:= TRUE;
 btnAssociate.Left:= 17;
 ...
end;
重要: 当您创建组件时,您需要在构造函数过程中使用这些行,以避免“控件没有父窗口”

继承的创建(AOOwner);
父项:=TWinControl(AOOwner)

我想你需要一个家长窗口。SSCE会帮忙的。嗨,大卫。我知道。但是他们(Embarcadero在他们的文章中)是如何做到的呢?@J.-一个人不应该在构造器中设置专利!您可以回答自己的问题,而不是在问题中发布新代码。@Jerrydoge-我知道。我在等待一个可能更好的答案。例如,为什么代码在NavCabeRo的文章中,而不是在我的代码中工作。如果你在CreateWindowHandle创建实例,可以考虑在它的对应部分中销毁:DestroyWindowHandle。最好只对需要窗口句柄的东西使用CreateWnd/WindowHandle。@SertacAkyuz-我不明白。我必须手动释放BTNASociate(和其他控件)?所有者设置为Self,因此Self应释放btnasociate。对吧?你没必要。但是,如果出于任何原因重新创建了groupbox的窗口,则您将拥有没有任何引用的活动按钮,只有当groupbox被销毁时,这些按钮才会被销毁。请像以前一样在OnCreate中创建按钮。仅使用“CreateWindowHandle”设置需要窗口句柄的属性,如设置客户端宽度/高度。我的评论只是一些注释。重要的是一些操作的手柄要求。请将您喜欢的任何内容整合到评论中的答案中。