Duplicating components at Run-Time

后端 未结 4 2026
鱼传尺愫
鱼传尺愫 2020-12-06 11:03

Is there a simple way to duplicate all child components under parent component, including their published properties?

For example:

  • TPanel
    • TLa
4条回答
  •  醉酒成梦
    2020-12-06 11:14

    It's actually fairly easy to duplicate existing components at runtime. The difficult part is to copy all of their published properties to the new (duplicated) objects.

    I'm sorry, but my code example is in C++Builder. The VCL is the same, just a different language. It shouldn't be too much trouble to translate it Delphi:

    for (i = 0; i < ComponentCount; ++i) {
        TControl *Comp = dynamic_cast(Components[i]);
        if (Comp) {
            if (Comp->ClassNameIs("TLabel")) {
                TLabel *OldLabel = dynamic_cast(Components[i]);
                TLabel *NewLabel = new TLabel(this);  // new label
                // copy properties from old to new
                NewLabel->Top = OldLabel->Top;
                NewLabel->Left = OldLabel->Left;
                NewLabel->Caption = Oldlabel->Caption
                // and so on...
            } else if (Comp->ClassNameIs("TPanel")) {
                // copy a TPanel object
            }
    

    Maybe somebody has a better method of copying all of the published properties of the old control to the new one.

提交回复
热议问题