我的表格上有一个水平滚动框项目。运行程序后,我将获得一个json字符串,其中包含必须位于水平滚动框中的项目列表。我必须动态添加它们。
我找到了这个功能:
HorzScrollBox1.AddObject();
但是这个
我有两个问题:
1)如何将新对象添加到此?
2)我可以克隆现有图像并将其添加到列表的末尾吗?
答案 0 :(得分:1)
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;