function GetCtrls(Control: TWinControl; List: TStringList): Boolean;
var
i: Integer;
obj: TWinControl;
begin
for i := 0 to Control.ControlCount - 1 do
begin
obj := TWinControl(Control.Controls[i]);
List.Add(obj.Name);
//如果控件中包含其他控件, 则递归调用
if obj.ControlCount > 0 then
begin
GetCtrls(obj, List);
end;
end;
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
list: TStringList;
begin
list := TStringList.Create;
GetCtrls(Self, list); {调用函数}
ShowMessage(list.Text); {显示窗体中所有控件的名称}
list.Free;
end;