gpt4 book ai didi

delphi - "Control ' ' has no parent window"错误

转载 作者:行者123 更新时间:2023-12-03 15:14:33 24 4
gpt4 key购买 nike

当我将控件放在表单上时,出现此错误。错误出现在这里:

  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;

答案可能是“标题需要有效的窗口句柄”。正确的?然而,David Intersimone ( here ) 的一篇文章说,在构造函数中设置 Caption 是可以的。

  1. 这篇文章有错吗?
  2. 我应该将代码(Caption 和 TButton.Create)移到 CreateWnd 中(因为 AfterConstruction 不是一个好地方)吗?问题是 CreateWnd 可以被多次调用:“当首次创建控件或必须销毁并重新创建底层屏幕对象以反射(reflect)属性更改时,会自动调用 CreateWnd。”
<小时/>

更新:
按照 J... 的建议,添加 (aOwner: TComponent) 到构造函数的声明(在实现中)后,错误移至下一行 (clientheight:= 90);

最佳答案

我将代码移至 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;

关于delphi - "Control ' ' has no parent window"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23316113/

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