【问题标题】:Find all the controls that are visible to the user查找用户可见的所有控件
【发布时间】:2012-09-18 11:18:54
【问题描述】:

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

【问题讨论】:

  • “即”之前的部分与后面的部分不完全匹配。例如,在它之前,“控件”不需要窗口化,它们可以被禁用。之后,它们必须同时被窗口化和启用。

标签: delphi delphi-xe2


【解决方案1】:

由于您写的是要列出可以通过 Tab 键访问的控件,因此我假设您在谈论窗口控件。

那么你可以简单地做

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;

如果您知道表单拥有它的所有子控件并且没有其他控件。否则,你必须这样做

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    相关资源
    最近更新 更多