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

  • 相关阅读:
    如何用伪类画出一个三角形
    关于昨天遇到题目的一点随笔
    opacity与rgba
    选择框脚本_移动/重排选项 P435
    文字等宽
    CSS3风骚渐变
    表单序列化 P436
    选择框脚本_添加/删除选项 P434
    选择框脚本_用事件选中选项,获取选中项信息 P432
    文本框组脚本_自动切换焦点“例如加区号和分机号的电话号码文本框组” P426
  • 原文地址:https://www.cnblogs.com/findumars/p/6706625.html
Copyright © 2011-2022 走看看