|
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 <> nil) and (Parent.Children <> nil) then 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 <> nil) and (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