为什么总是提示"Control '' has no parent window!"错误?(100分)

Z

zqw0117

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个控件,是从TGraphicControl继承下来的,但现在我需要将它转换成
ActiveX(.ocx)控件。但要把VCL控件转换成ActiveX控件的话必须保证被转换
的控件是从TWinControl继承的。这个继承关系我无法改变,于是我想用一个容器
控件(就是一个TWinControl控件)包含这个从TGraphicControl继承下了的控件
做成另外一个合成控件,然后再转换成ActiveX控件。

想法到可以,但出现的问题是,当点击new->acitve-ActiveX Control的时候,总是提示:

"Control '' has no parent window"错误

我不知该如何修正这个错误,请各位大虾指教一下,谢谢!

下面是我的部分代码:

type
TDropChangingEvent = procedure (Sender: TObject
var Allowed: Boolean) of Object;

TColorPicker = class(TWinControl)
private
{ Private declarations }
FButton: TColorPickerButton;//这个就是那个从TGraphicControl继承下来的控件
...
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams)
override;
...
procedure Loaded
override;
public
{ Public declarations }
constructor Create(AOwner: TComponent)
override;
destructor Destroy
override;
...
published
{ Published declarations }
...
end;

implementation

constructor TColorPicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Self.SetParent(AOwner as TWinControl);//这一句好像不能去掉,否则无法在窗体上放置该控件
Self.SetParentComponent(AOwner);//这个不知是否有用
Self.BevelInner := bvNone;
Self.BevelOuter := bvNone;
Self.Caption := '';
FButton := TColorPickerButton.Create(Self);
FButton.Parent := Self;
...
end;

destructor TColorpicker.Destroy;
begin
FButton.Free;
inherited Destroy;
end;

procedure TColorPicker.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
end;


差不多就只有这么一点点改动,但就是无法成功转换,请帮帮我,谢谢!
 
原来我做activexform时也曾经遇到过,关注。
 
你可以试试把
FButton := TColorPickerButton.Create(Self);
FButton.Parent := Self;
放在InitilizeControl过程中。
另外,把DELPHI本身当作是host application,就可以单步运行调试代码了
 
to szf:
我无法将上面的代码移植到InitilizeControl中,因为出现问题的时候ActiveX控件自动创建
过程还没有完成!也就是说,点击new->acitveX->ActiveX Control命令后,设置好选项和继承
条件后,点击确定按钮,这时Delphi会自动生成相关文件源码,可是就是在这个生成源码
的过程中出现上述"Control '' has not parent window"错误提示的,而且最终的*_Imt.pas
文件根本没有生成!哪里能够找到InitilizeControl的代码?这样不行!

至于把Delphi作为host application调试我也做不到,我的CPU是Celeron300的,96M内存,
无法同时启动两个Delphi!

还有,之前我用ActiveForm作为容器做过一次转换,那个转换没有问题。可是我不能用ActiveForm
因为做好的控件总是有奇怪的边框线条。

谢谢大家,请帮帮我!
 
那好象是delphi的向导问题喔,这可搞不定,最好的办法是重装了 -_-!
 
不是Delphi向导的问题,我可以转换任何其它控件,如TPanel,TCheckBox等,只是这个控件
转换有问题。

哪位知道问题原因的大虾,请帮帮我。
 
我认为你写的构建函数很不安全,AOwner只继承自TComponent,如果不是TWinControl就会有异常了。另外,从系统报错的信息看是为FButton设置Parent属性是出的错,constructor Create(AOwner: TComponent);是创建TComponent的构建函数,这是对象的窗台还没有创建,应该报需要用到窗体的代码应该写在重用TWincontrol的CreateWnd;的方法中。
 
to tata1:
谢谢您的提醒,不过是不是因为ocx控件一定只能放到别的编程环境中的窗体上,所以这样
的TComponent应该在应用的时候没有问题?

另外,您的意思是重载CreateWnd方法吗?如果是,我需要在这个重载方法中写什么?是不是
直接将上面的

Self.SetParent(AOwner as TWinControl
Self.SetParentComponent(AOwner);

放到CreateWnd方法里面?
 
唉,我自己解决了。
谢谢大家。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
663
import
I
I
回复
0
查看
510
import
I
顶部