13

公開されたプロパティを含め、親コンポーネントの下にすべての子コンポーネントを複製する簡単な方法はありますか?

例えば:

  • Tパネル
    • Tラベル
    • TEdit
    • TListView
    • TSpecialClassX

もちろん、最も重要な要素は、通常の状況ではコードを変更せずに、TPanel にドロップした新しいコンポーネントを複製する必要があることです。

RTTI については聞いたことがありますが、実際に使用したことはありません。何か案は?

4

4 に答える 4

9

親のコントロールを介してループで複製コンポーネントを作成した後、「実行時にビジュアルコンポーネントを置き換える」への回答から CLoneProperties ルーチンを適切に使用できます。

更新:いくつかの作業コード....

. あなたの質問から、WinControl に含まれるコントロールを複製したいと思います (親は TWinControl であるため)。
. 複製されたコントロールをオリジナルと同じイベント ハンドラーでフックするかどうかわからなかったので、そのためのオプションを作成しました。
. また、複製されたコントロールに適切で意味のある名前を付けたい場合があります。

uses
  TypInfo;

procedure CloneProperties(const Source: TControl; const Dest: TControl);
var
  ms: TMemoryStream;
  OldName: string;
begin
  OldName := Source.Name;
  Source.Name := ''; // needed to avoid Name collision
  try
    ms := TMemoryStream.Create;
    try
      ms.WriteComponent(Source);
      ms.Position := 0;
      ms.ReadComponent(Dest);
    finally
      ms.Free;
    end;
  finally
    Source.Name := OldName;
  end;
end;

procedure CloneEvents(Source, Dest: TControl);
var
  I: Integer;
  PropList: TPropList;
begin
  for I := 0 to GetPropList(Source.ClassInfo, [tkMethod], @PropList) - 1 do
    SetMethodProp(Dest, PropList[I], GetMethodProp(Source, PropList[I]));
end;

procedure DuplicateChildren(const ParentSource: TWinControl;
  const WithEvents: Boolean = True);
var
  I: Integer;
  CurrentControl, ClonedControl: TControl;
begin
  for I := ParentSource.ControlCount - 1 downto 0 do
  begin
    CurrentControl := ParentSource.Controls[I];
    ClonedControl := TControlClass(CurrentControl.ClassType).Create(CurrentControl.Owner);
    ClonedControl.Parent := ParentSource;
    CloneProperties(CurrentControl, ClonedControl);
    ClonedControl.Name := CurrentControl.Name + '_';
    if WithEvents then
      CloneEvents(CurrentControl, ClonedControl);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DuplicateChildren(Panel1);
end;
于 2008-10-27T08:18:25.327 に答える
6

このページを読んでください

Delphi のランタイム型情報 - 何かできることはありますか?

コンポーネントから別のコンポーネントへのプロパティのコピーのセクションに注意してください

これには、プロシージャを備えたRTTIUnitというユニットがあり、必要なことの一部を実行しているように見えますが、追加のコードなしで子コンポーネントをコピーするとは思いません。 (ここに貼り付けて大丈夫だと思います...)

procedure CopyObject(ObjFrom, ObjTo: TObject);    
  var
PropInfos: PPropList;
PropInfo: PPropInfo;
Count, Loop: Integer;
OrdVal: Longint;
StrVal: String;
FloatVal: Extended;  
MethodVal: TMethod;
begin
//{ Iterate thru all published fields and properties of source }
//{ copying them to target }

//{ Find out how many properties we'll be considering }
Count := GetPropList(ObjFrom.ClassInfo, tkAny, nil);
//{ Allocate memory to hold their RTTI data }
GetMem(PropInfos, Count * SizeOf(PPropInfo));
try
//{ Get hold of the property list in our new buffer }
GetPropList(ObjFrom.ClassInfo, tkAny, PropInfos);
//{ Loop through all the selected properties }
for Loop := 0 to Count - 1 do
begin
  PropInfo := GetPropInfo(ObjTo.ClassInfo, PropInfos^[Loop]^.Name);
 // { Check the general type of the property }
  //{ and read/write it in an appropriate way }
  case PropInfos^[Loop]^.PropType^.Kind of
    tkInteger, tkChar, tkEnumeration,
    tkSet, tkClass{$ifdef Win32}, tkWChar{$endif}:
    begin
      OrdVal := GetOrdProp(ObjFrom, PropInfos^[Loop]);
      if Assigned(PropInfo) then
        SetOrdProp(ObjTo, PropInfo, OrdVal);
    end;
    tkFloat:
    begin
      FloatVal := GetFloatProp(ObjFrom, PropInfos^[Loop]);
      if Assigned(PropInfo) then
        SetFloatProp(ObjTo, PropInfo, FloatVal);
    end;
    {$ifndef DelphiLessThan3}
    tkWString,
    {$endif}
    {$ifdef Win32}
    tkLString,
    {$endif}
    tkString:
    begin
      { Avoid copying 'Name' - components must have unique names }
      if UpperCase(PropInfos^[Loop]^.Name) = 'NAME' then
        Continue;
      StrVal := GetStrProp(ObjFrom, PropInfos^[Loop]);
      if Assigned(PropInfo) then
        SetStrProp(ObjTo, PropInfo, StrVal);
    end;
    tkMethod:
    begin
      MethodVal := GetMethodProp(ObjFrom, PropInfos^[Loop]);
      if Assigned(PropInfo) then
        SetMethodProp(ObjTo, PropInfo, MethodVal);
    end
  end
end
finally
  FreeMem(PropInfos, Count * SizeOf(PPropInfo));
end;
end;
于 2008-10-27T05:27:00.023 に答える
4

ソース コンポーネントをストリームに書き込んで、それをターゲット コンポーネントに読み戻すことができます。

MemStream := TMemoryStream.Create;
try
  MemStream.WriteComponent(Source);
  MemStream.Position := 0;
  MemStream.ReadComponent(Target);
finally
  MemStream.Free;
end;

ただし、コンポーネント名が重複していると問題が発生する場合があります。

于 2008-10-27T21:12:39.443 に答える
0

実際、実行時に既存のコンポーネントを複製するのはかなり簡単です。難しいのは、公開されたすべてのプロパティを新しい (複製された) オブジェクトにコピーすることです。

申し訳ありませんが、私のコード例は C++Builder です。VCL は同じで、言語が異なるだけです。それを Delphi に翻訳するのはそれほど難しいことではありません。

for (i = 0; i < ComponentCount; ++i) {
    TControl *Comp = dynamic_cast<TControl *>(Components[i]);
    if (Comp) {
        if (Comp->ClassNameIs("TLabel")) {
            TLabel *OldLabel = dynamic_cast<TDBEdit *>(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
        }

古いコントロールの公開されたすべてのプロパティを新しいコントロールにコピーするより良い方法があるかもしれません。

于 2008-10-27T04:07:26.237 に答える