[BCB] 遍历界面所有元素

(TComponent类, 声明) Components
(TWinControl类, 声明) Controls

(1) 对Panel1中控件有效:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int midLoc=Width/2;
TControl * ChildControl;
for(int i=0; i<Panel1-> ControlCount; i++)
{//遍历Controls数组,ControlCount是数组元素个数
ChildControl = Panel1-> Controls[i];
ChildControl->Left = midLoc-ChildControl->Width/2;
}
}

(2) 对窗口上的全部控件有效:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int midLoc=Width/2;
TComponent * ChildComponent;
for(int i=0; i<ComponentCount; i++)
{//遍历Components数组,ComponentCount是数组元素个数
ChildComponent = Components[i];
ChildComponent->Left = midLoc-ChildComponent->Width/2;
}
}

你可能感兴趣的:(遍历)