Add object: there are two ways - set Parent
property of children or execute AddObject
method of parent. Parent
property setter has some checks, and call AddObject
.
Depending on control class, child object can be added to Controls
collection of the control or to its Content
private field. Not all classes provide access to this field (in some cases you can use workaround, like in this question). HorzScrollBox has this field in public section.
So, if you want to clone existing image, you must:
Get existing image from Content
of HorzScrollBox
Create new image and set it properties
Put new image to HorzScrollBox.
For example:
procedure TForm2.btnAddImgClick(Sender: TObject);
function FindLastImg: TImage;
var
i: Integer;
tmpImage: TImage;
begin
Result:=nil;
// search scroll box content for "most right" image
for i := 0 to HorzScrollBox1.Content.ControlsCount-1 do
if HorzScrollBox1.Content.Controls[i] is TImage then
begin
tmpImage:=TImage(HorzScrollBox1.Content.Controls[i]);
if not Assigned(Result) or (Result.BoundsRect.Right < tmpImage.BoundsRect.Right) then
Result:=tmpImage;
end;
end;
function CloneImage(SourceImage: TImage): TImage;
var
NewRect: TRectF;
begin
Result:=TImage.Create(SourceImage.Owner);
Result.Parent:=SourceImage.Parent;
// Copy needed properties. Assign not work for TImage...
Result.Align:=SourceImage.Align;
Result.Opacity:=SourceImage.Opacity;
Result.MultiResBitmap.Assign(SourceImage.MultiResBitmap);
// move new image
NewRect:= SourceImage.BoundsRect;
NewRect.Offset(NewRect.Width, 0); // move rect to right.
Result.BoundsRect:=NewRect;
end;
begin
CloneImage(FindLastImg);
end;