var i: Integer; begin for i := 0 to ComponentCount - 1 do begin ShowMessage(Components[i].ClassName); end; end; 这个可以显示所有控件的类名,你修改一下就可以得到了。
// 返回窗体上不可见控件的个数 function TForm1.GetHideControlCount(Control: TWinControl): Integer; var I: Integer; begin Result := 0; with Control do for I := 0 to ControlCount - 1 do begin if csAcceptsControls in Controls[I].ControlStyle then Result := Result + GetHideControlCount(TWinControl(Controls[I])) else if Controls[I].Visible = False then Inc(Result); //可以修改这里达到你的要求 end; end;procedure TForm1.Button2Click(Sender: TObject); var Count: Integer; begin Count := GetHideControlCount(Self); ShowMessage(IntToStr(Count)); end;
什么意思?是遍历不可见的控件拉!不过,我上面的代码漏掉了容器控件自身。应该改为: function TForm1.GetHideControlCount(Control: TWinControl): Integer; var I: Integer; begin Result := 0; with Control do for I := 0 to ControlCount - 1 do begin if Controls[I].Visible = False then Inc(Result); if csAcceptsControls in Controls[I].ControlStyle then Result := Result + GetHideControlCount(TWinControl(Controls[I])); end; end;procedure TForm1.Button2Click(Sender: TObject); var Count: Integer; begin Count := GetHideControlCount(Self); ShowMessage(IntToStr(Count)); end;
var I: Integer; tmpComponent: TComponent; begin for I := 0 to Self.ComponentCount - 1 do begin tmpComponent := Self.Components[I]; if not tmpComponent.InheritsFrom(TControl) then begin ShowMessage(tmpComponent.Name); end; end; end;不知道楼主所说的不可见 包不包括从TGraphicControl继承下来的(如TImage).上面的例子是不包括的.(LZ的表达有点问题: TComponent继承的叫组件, 不叫控件,控件都从TControl下继承的)
var
i: Integer;
begin
for i := 0 to ComponentCount - 1 do
begin
ShowMessage(Components[i].ClassName);
end;
end;
这个可以显示所有控件的类名,你修改一下就可以得到了。
// 返回窗体上不可见控件的个数
function TForm1.GetHideControlCount(Control: TWinControl): Integer;
var
I: Integer;
begin
Result := 0;
with Control do
for I := 0 to ControlCount - 1 do
begin
if csAcceptsControls in Controls[I].ControlStyle then
Result := Result + GetHideControlCount(TWinControl(Controls[I]))
else if Controls[I].Visible = False then
Inc(Result); //可以修改这里达到你的要求
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
Count: Integer;
begin
Count := GetHideControlCount(Self);
ShowMessage(IntToStr(Count));
end;
function TForm1.GetHideControlCount(Control: TWinControl): Integer;
var
I: Integer;
begin
Result := 0;
with Control do
for I := 0 to ControlCount - 1 do
begin
if Controls[I].Visible = False then
Inc(Result);
if csAcceptsControls in Controls[I].ControlStyle then
Result := Result + GetHideControlCount(TWinControl(Controls[I]));
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
Count: Integer;
begin
Count := GetHideControlCount(Self);
ShowMessage(IntToStr(Count));
end;
I: Integer;
tmpComponent: TComponent;
begin
for I := 0 to Self.ComponentCount - 1 do
begin
tmpComponent := Self.Components[I];
if not tmpComponent.InheritsFrom(TControl) then
begin
ShowMessage(tmpComponent.Name);
end;
end;
end;不知道楼主所说的不可见 包不包括从TGraphicControl继承下来的(如TImage).上面的例子是不包括的.(LZ的表达有点问题: TComponent继承的叫组件, 不叫控件,控件都从TControl下继承的)