zoukankan      html  css  js  c++  java
  • FMX 动态创建 和 销毁(释放free) 对象

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    function FindChild(Name: string; Parent: TFmxObject): TFmxObject;
    var
      I: Integer;
      Child: TFmxObject;
    begin
      Result := nil;
      if (Parent <> niland (Parent.Children <> nilthen
      begin
        for I := Parent.Children.Count - 1 downto 0 do
        begin
          Child := TFmxObject(Parent.Children[I]);
          if Child.Name = Name then
          begin
            Result := Child;
            break;
          end;
        end;
      end;
    end;
    procedure FreeAndNilFmxObject(var Obj: TFmxObject);
    begin
      if Obj <> nil then
      begin
        Obj.Parent := nil;
        Obj.SetRoot(nil);
        FreeAndNil(Obj);
      end;
    end;
    var
      AIndex : Integer 0;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      A: TButton;
      B: TFmxObject;
    begin
      ReportMemoryLeaksOnShutdown := True;
      B := FindChild('A', Self);
      if (B <> niland (B is TButton) then
      begin
        A := B as TButton;
        B := nil;
    //    //后面 FormDestroy 的代码不一样,但效果一样。
        FreeAndNilFmxObject(TFmxObject(A));
      end;
    //  if (B <> nil) and (B is TButton) then
    //  begin
    //    A := B as TButton;
    //    B.Parent := nil;
    //    B.SetRoot(nil);
    //    B := nil;
    //    FreeAndNil(A);
    //  end;
      inc(AIndex);
      A := TButton.Create(nil);
      A.Name := 'A';
      A.Position.Y := AIndex * A.Height;
      A.Text := '按钮' + AIndex.ToString;
      A.Parent := Self;
    end;
    procedure TForm1.FormDestroy(Sender: TObject);
    var
      B: TFmxObject;
    begin
      ReportMemoryLeaksOnShutdown := True;
      B := FindChild('A', Self);
      //这样写更简单些。
      FreeAndNilFmxObject(B);
    end;

    http://www.delphi6.com/thread-68.htm

  • 相关阅读:
    【leetcode】1630. Arithmetic Subarrays
    【leetcode】1629. Slowest Key
    【leetcode】1624. Largest Substring Between Two Equal Characters
    【leetcode】1620. Coordinate With Maximum Network Quality
    【leetcode】1619. Mean of Array After Removing Some Elements
    【leetcode】1609. Even Odd Tree
    【leetcode】1608. Special Array With X Elements Greater Than or Equal X
    【leetcode】1603. Design Parking System
    【leetcode】1598. Crawler Log Folder
    Java基础加强总结(三)——代理(Proxy)Java实现Ip代理池
  • 原文地址:https://www.cnblogs.com/findumars/p/6706625.html
Copyright © 2011-2022 走看看