My Environment: C++ Builder XE4how to copy all the TLabels parented with a TPanel on delphi to another TPanel?
我想在C++生成器中实现上面的代码。
我不知道如何在C++生成器中实现下面的内容。
if ParentControl.Controls[i] is TLabel then是否有任何函数可以获得TLabel或其他类型的类型?
发布于 2015-10-11 17:43:10
使用dynamic_cast
if (dynamic_cast<TLabel*>(ParentControl->Controls[i]) != NULL)下面是该代码的翻译:
void __fastcall CopyLabels(TWinControl *ParentControl, TWinControl *DestControl)
{
for(int i = 0; i < ParentControl->ControlCount; ++i)
{
if (dynamic_cast<TLabel*>(ParentControl->Controls[i]) != NULL)
{
TLabel *ALabel = new TLabel(DestControl);
ALabel->Parent = DestControl;
ALabel->Left = ParentControl->Controls[i]->Left;
ALabel->Top = ParentControl->Controls[i]->Top;
ALabel->Width = ParentControl->Controls[i]->Width;
ALabel->Height = ParentControl->Controls[i]->Height;
ALabel->Caption= static_cast<TLabel*>(ParentControl->Controls[i])->Caption;
//you can add manually more properties here like font or another
}
}
}尽管如此,这样做的效率会略高一些:
void __fastcall CopyLabels(TWinControl *ParentControl, TWinControl *DestControl)
{
int count = ParentControl->ControlCount;
for(int i = 0; i < count; ++i)
{
TLabel *SourceLabel = dynamic_cast<TLabel*>(ParentControl->Controls[i]);
if (SourceLabel != NULL)
{
TLabel *ALabel = new TLabel(DestControl);
ALabel->Parent = DestControl;
ALabel->SetBounds(SourceLabel->Left, SourceLabel->Top, SourceLabel->Width, SourceLabel->Height);
ALabel->Caption = SourceLabel->Caption;
//you can add manually more properties here like font or another
}
}
}发布于 2015-10-13 08:12:19
您可以将ClassType方法用作:
if(Controls[i]->ClassType() == __classid(TLabel))
{
...
}发布于 2015-10-11 02:39:42
我找到了ClassName()方法。
下面看上去很有效。
static bool isTCheckBox(TControl *srcPtr)
{
if (srcPtr->ClassName() == L"TCheckBox") {
return true;
}
return false;
}https://stackoverflow.com/questions/33060775
复制相似问题