问题描述
我是组件创建的新手,正在尝试使用Delphi的标准问题VCL创建一些我自己的定制派生组件。
I am new to component creation and was experimenting with creating some of my own custom derived components using the standard issue VCL from Delphi.
我以为我可以混合两个组件在一起,创建一个单一的。下面我到目前为止,这个想法是将TImage放在TScrollBox中:
I thought I could mix two components together, to create one singular one. Take below what I have so far, the idea is to put a TImage inside a TScrollBox:
unit MyComponent;
interface
uses
Windows,
Classes,
Controls,
Forms,
ExtCtrls;
type
TMyPanel = class(TScrollBox)
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TMyPanel]);
end;
{ TMyPanel }
constructor TMyPanel.Create(AOwner: TComponent);
var
AImage: TImage;
begin
inherited Create(AOwner);
AImage := TImage.Create(AOwner);
AImage.Align := alClient;
AImage.Parent := Self;
end;
destructor TMyPanel.Destroy;
begin
inherited;
end;
end.
如果我将上面的代码编译成一个包,结果是如下所示:
If I compile and install the above into a package, the result is as shown below:
问题
我希望我的组件被注册为一个零件。但组件应该是TScrollBox和TImage的组合。主要组件将是TScrollBox,但它现在也可以访问TImage的属性和事件等。
I would like my component to be registered as one single component. But the component should be a combination of both TScrollBox and TImage. The main component will be the TScrollBox, but it should now have access to the properties and events etc of the TImage aswell.
例如,TMyPanel可以共享TImage的属性和TScrollBox在一起:
For example, TMyPanel could share the properties of TImage and TScrollBox together:
- AutoSize
- BorderStyle
- HorzScrollBar
- ParentBackground
- 图片
- VertScrollBar
- AutoSize
- BorderStyle
- HorzScrollBar
- ParentBackground
- Picture
- VertScrollBar
我认为完全写一个新的组件来做上述的行为是太过分了,我真的不知道从哪里开始。如果这可以实现,您可以创建一些有趣的组件,组合成一个,但保留原有的属性,方法和事件等。
I think it would be overkill to completely write a new component to do the behavior described above, that and I really wouldn't know where to begin. If this can be accomplished you could create some interesting components that are combined into one, but keep there original properties, methods and events etc.
这是我想要实现的这里有一个TImage在TScrollBox中。
This is what I want to achieve here with a TImage inside a TScrollBox.
解决方案
Uwe Raabe按预期工作。 TImage现在注册在TScrollBox中,但是显示为一个组件。 TImage的属性在Object Inspector中显示为Image。 >这将显示TImage的属性:)
The answer shown by Uwe Raabe works as expected. The TImage is now registered inside the TScrollBox, but appears as one component. The properties of the TImage are shown in the Object Inspector as Image. > which will reveal the properties of TImage :)
推荐答案
您应该使该映像成为TMyPanel的子组件:
You should make the image a subcomponent of TMyPanel: SetSubComponent
更新:这是一个例子
unit MyComponent;
interface
uses
System.Classes,
VCL.Controls,
VCL.Forms,
VCL.ExtCtrls;
type
TMyPanel = class(TScrollBox)
private
FImage: TImage;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Image: TImage read FImage;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TMyPanel]);
end;
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FImage := TImage.Create(Self);
FImage.SetSubComponent(true);
FImage.Align := alClient;
FImage.Parent := Self;
end;
destructor TMyPanel.Destroy;
begin
inherited;
end;
end.
这篇关于组件创建 - 将组件连接在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!