找到用户可见的所有控件

nor*_*aul 8 delphi delphi-xe2

如何在表单上找到用户当前可见的所有控件?列出所有可以选中的控件并且不会从视图中隐藏(例如,在不可见的选项卡表上).

And*_*and 15

既然你写了你要列出你可以选中的控件,我假设你在谈论窗口控件.

然后你就可以做到

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to ComponentCount - 1 do
    if Components[i] is TWinControl then
      if TWinControl(Components[i]).CanFocus then
        Memo1.Lines.Add(Components[i].Name)
end;
Run Code Online (Sandbox Code Playgroud)

如果您知道该表单拥有其所有子级而没有其他控件.否则,你必须这样做

procedure AddVisibleChildren(Parent: TWinControl; Memo: TMemo);
var
  i: Integer;
begin
  for i := 0 to Parent.ControlCount - 1 do
    if Parent.Controls[i] is TWinControl then
      if TWinControl(Parent.Controls[i]).CanFocus then
      begin
        Memo.Lines.Add(Parent.Controls[i].Name);
        AddVisibleChildren(TWinControl(Parent.Controls[i]), Memo);
      end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  AddVisibleChildren(Self, Memo1);
end;
Run Code Online (Sandbox Code Playgroud)