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

  • 相关阅读:
    我要好offer之 二叉树大总结
    我要好offer之 字符串相关大总结
    楼层扔鸡蛋问题[转]
    Linux System Programming 学习笔记(十一) 时间
    Linux System Programming 学习笔记(十) 信号
    Linux System Programming 学习笔记(九) 内存管理
    Linux System Programming 学习笔记(八) 文件和目录管理
    Linux System Programming 学习笔记(七) 线程
    Linux System Programming 学习笔记(六) 进程调度
    APUE 学习笔记(十一) 网络IPC:套接字
  • 原文地址:https://www.cnblogs.com/findumars/p/6706625.html
Copyright © 2011-2022 走看看