如何知道控件是TLabel?

时间:2015-10-11 01:49:31

标签: c++builder

My Environment: C++ Builder XE4

how to copy all the TLabels parented with a TPanel on delphi to another TPanel?

我想在C ++ Builder中实现上面的代码。

我不知道如何在C ++ Builder中实现如下。

if ParentControl.Controls[i] is TLabel then

是否有任何函数可以将类型作为TLabel或其他类型?

3 个答案:

答案 0 :(得分:3)

您可以将 ClassType 方法用作:

if(Controls[i]->ClassType() == __classid(TLabel))
{
    ...
}

答案 1 :(得分:1)

使用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 
        }
    }
}

答案 2 :(得分:0)

我找到了ClassName()方法。

以下似乎有效。

static bool isTCheckBox(TControl *srcPtr)
{
    if (srcPtr->ClassName() == L"TCheckBox") {
        return true;
    }
    return false;
}