|
MemoryStream 이든 뭐든 stream 만들어서 WriteComponent 시키고
이쪽에서 ReadComponent 하시면....
아래 코드는 Delphi 코드지만 C++ 로 금방 바꾸실수 있으실듯. 참조하세요...
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;
레쓰비 님이 쓰신 글 :
: 동적으로 생성한 에디트 박스를 기존 팔레트에서 생성한 에디트 박스의 모든 속성과 같게 하고 싶은데요...
: 아래 코드와 같이 일일이 하나하나 다 지정하는 방법 말고 간단히 하는방법이 없을까요??
:
: // ((TEdit *)tcmp_edit) --> 동적생성 EditBox 를 나타냅니다.
: // EditBox1 --> 팔레트에서 생성한 EditBox 를 나타냅니다.
:
: ((TEdit *)tcmp_edit)->Width = EditBox1->Width;
: ((TEdit *)tcmp_edit)->Height = EditBox1->Height;
: ...
: ...
: ...
: ...
: ...
|