AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > C++

在BCB中使用VCL控件数组

51自学网 http://www.51zixue.net

我的《BCB中使用VCL控件数组》中,提到了用TList来实现时无法释放资源的问题,结果今天就得到了答案,邬彦华等等网友都指教了TList实现的释放方法,定义代码同前文:

for (int i=1;i<=ButtonCount;i++)
{
TSpeedButton *spdBtn=new TSpeedButton(this);
spdBtn->Parent=ScrollBox;//指定父控件
spdBtn->Caption=IntToStr(i);
spdBtn->Width=80;
spdBtn->Height=80;
spdBtn->OnClick=ButtonClick;
spdBtn->Left=intLeft;
spdBtn->Top=intTop;
spdBtn->GroupIndex=1;
spdBtn->Flat=true;
intLeft=intLeft+80+intSpace;
if (i%LineCount==0)
{
intTop=intTop+80+intSpace;
intLeft=intSpace;
}
buttons->Add(spdBtn);//buttons是一个TList的指针
}
释放资源的代码如下:

int num = button->Count;
for(int i=0;i<num;i++)
{
delete (TSpeedButton *)button->Items[i];
}
其实说穿了就是删除TList的每一项,不过因为TList->Items的类型是void *,在C/C++中,void *可匹配任何类型,所以只要加一个强制类型转换(TSpeedButton *)就可以了,当然用(TObject *)等也是可以的,因为TObject是VCL中所有类的基类,而基类的指针是可以指向它的直接或间接子类的。

两种方法的比较:我上次的方法可说是典型的C++解决方法,而这种方法可说是C++Builder的解决方法,用双重指针比较灵活、高效,但大家都知道,多重指针可不那么好懂,想当初我曾用过4重指针(不要用那么目光看着我!),到后来我自已都糊涂了。而这次的方法比较好懂,并且能用TList类的方法,使用比较方便,但不如上一种高效,总的来说,两种方法各有优劣,具体使用那一种,就看个人的喜好了。

 
 

上一篇:使用C++&nbsp;Buider制作自绘画列表框  下一篇:在C++Builder中使用Delphi控件