Delphi 动态添加控件和批量添加控件

delphi4年前 (2021)发布 admin
467 0

添加控件

var
  t_image: TIMAGE;
begin         //生成1个控件
  t_image := timage.Create(self);
  t_image.Parent := Form1;
  t_image.Picture.LoadFromFile('6.png');
  t_image.Height := 100;
  t_image.Width := 100;
  t_image.top := 100;
  t_image.left := 100;

批量添加控件

var
  t_image: TIMAGE;
  i: Integer;
begin         //生成多个个控件

  for i := 0 to c - 1 do
  begin
    t_image := timage.Create(self);
    t_image.Parent := Form1;
    t_image.Picture.LoadFromFile('6.png');
    t_image.Height := 100;
    t_image.Width := 100;
    t_image.top := 100;
    t_image.left := i * 100 + 10;
    t_image.Name := 'img' + IntToStr(i + 1);
    t_image.Anchors := [akLeft, akTop, akRight, akBottom];
    t_image.Stretch := True;
    mmo2.Lines.Add(t_image.Name);
  end;

批量设置动态添加的控件的属性

   for i := 1 to c do
    begin

      TIMAGE(FindComponent('img' + IntToStr(i))).Left := StrToInt(MyIni.ReadString('ZZ_Weizhi', 'zz' + IntToStr(i) + '_Left', ''));
      TIMAGE(FindComponent('img' + IntToStr(i))).top := StrToInt(MyIni.ReadString('ZZ_Weizhi', 'zz' + IntToStr(i) + '_top', ''));
      TIMAGE(FindComponent('img' + IntToStr(i))).width := StrToInt(MyIni.ReadString('ZZ_Weizhi', 'zz' + IntToStr(i) + '_width', ''));
      TIMAGE(FindComponent('img' + IntToStr(i))).Height := StrToInt(MyIni.ReadString('ZZ_Weizhi', 'zz' + IntToStr(i) + '_Height', ''));
    end;

删除动态添加的控件

var
  t_image: TIMAGE;
  i: Integer;
begin         //生成多个个控件
  for i := 0 to c - 1 do
  begin
    TIMAGE(FindComponent('img' + IntToStr(i + 1))).Free;

  end;

end;

© 版权声明

相关文章