Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

delphi - Component Creation - Joining Components Together?

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.

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:

enter image description here

enter image description here

Problem

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.

For example, TMyPanel could share the properties of TImage and TScrollBox together:

  • 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.

This is what I want to achieve here with a TImage inside a TScrollBox.

Solution

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 :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should make the image a subcomponent of TMyPanel: SetSubComponent

Update: Here is an example

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...