【问题标题】:firemonkey add item to HorzScrollBox on the flyfiremonkey 即时将项目添加到 HorzScrollBox
【发布时间】:2016-01-27 08:58:40
【问题描述】:

我的表单上有一个水平滚动框项目。运行程序后,我将获得一个 json 字符串,其中包含必须在水平滚动框中的项目列表。我必须动态添加它们。

例如我有这个: 在运行程序后?我必须一个新的图像。

我找到了功能: HorzScrollBox1.AddObject(); 但这需要一个参数

我有两个问题:

1)如何将新对象添加到此?

2)我可以克隆现有图像并将其添加到列表末尾吗?

【问题讨论】:

    标签: firemonkey delphi-xe8


    【解决方案1】:

    添加对象:有两种方法——设置子对象的Parent属性或执行父对象的AddObject方法。 Parent 属性设置器有一些检查,并调用 AddObject。 根据控件类,可以将子对象添加到控件的Controls 集合或其Content 私有字段中。并非所有类都提供对该字段的访问(在某些情况下,您可以使用解决方法,例如 in this question)。 HorzScrollBox 在 public 部分有这个字段。

    所以,如果你想克隆现有的图像,你必须:

    1. ContentHorzScrollBox 获取现有图像

    2. 创建新图像并设置属性

    3. 将新图像放入 HorzScrollBox。

    例如:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 2012-03-09
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多