Versuche gerade, ein TPagecontrol (2 TTabsheets + Controls darauf) zu klonen.
Das Klonen funktioniert soweit, nur die Controls auf den TTabsheets sind nicht sichtbar..
Habe schon versucht, die geklonten Controls sichtbar zu machen (visible = True). Bringt aber nichts.
Auch liegen die Controls IMO innerhalb des sichtbaren Bereichs...Was läuft hier falsch?
Anbei ein Demoprojekt zum selber nachvollziehen.
Nachtrag: Selbt wenn ich nach dem Klonen z.B einen TButton auf eines der neu erstellten TTabsheets
setze, wird es nicht angezeigt...
Codeauszug:
Delphi-Quellcode:
function CloneComponent(Source: TComponent;
aInstance: TComponent =
nil;
aOwner: TComponent =
nil;
aParent: TWinControl =
nil;
Options: TCloneOptions = []): TComponent;
const
Index: Integer = 0;
// static, needs $J+ state
var
I: Integer;
OrgName:
string;
Stream: TMemoryStream;
begin
Result :=
nil;
if not Assigned(Source)
then Exit;
// Create new instance when needed (or just use the one given).
if aInstance =
nil then
begin
if aOwner =
nil then
aOwner := Source.Owner;
Result := TComponentClass(Source.ClassType).Create(aOwner)
end
else
if aInstance.ClassType = Source.ClassType
then
Result := aInstance
else
raise Exception.Create('
<aInstance> = ' + aInstance.ClassName +
'
is not the same component class as' +
'
<Source> = ' + Source.ClassName);
// Although registering of normal classes shouldn't be neccessary
// it is required for some of the children like TToolButton, etc.
if Source
is TWinControl
then
with TWinControl(Source)
do
for I := 0
to ControlCount - 1
do
begin
try
RegisterClass(TPersistentClass(Controls[I].ClassType));
except
end;
end;
try
// Use new parent when one was given, otherwise use the one from <Source>.
if Source
is TControl
then
with TControl(Result)
do
if aParent <>
nil then
Parent := aParent
else
Parent := TControl(Source).Parent;
// Clone published properties.
OrgName := Source.
Name;
Stream := TMemoryStream.Create;
try
Source.
Name := '
CloneComponent' + IntToStr(
Index) + OrgName;
Stream.WriteComponent(Source);
Source.
Name := OrgName;
Stream.Position := 0;
Result := Stream.ReadComponent(Result);
finally
Stream.Free;
end;
// Try to set caption/text when <Source> is a control
// to the same caption/text as that of the <Source> control.
if Source
is TControl
then
if IsPublishedProp(Result, '
Caption')
or
IsPublishedProp(Result, '
Text')
then
with TControlCracker(Result)
do
if (csSetCaption
in ControlStyle)
and (
Name = Text)
then
Text := OrgName;
except
// If something went wrong only free the clone
// when it was created by this function.
if aInstance =
nil then Result.Free;
raise;
end;
// Clone all Children to the new instance as well?
if (coIncludeChildren
in Options)
and (Source
is TWinControl)
then
with TWinControl(Source)
do
for I := 0
to ControlCount - 1
do
if Controls[I].ClassName <> '
TToolButton'
then
CloneComponent(Controls[I],
nil,
nil, TWinControl(Result), Options);
if Index < MAXINT
then Inc(
Index)
else Index := 0;
end;