我正在尝试创建一个可以容纳某些可视组件的Delphi非可视组件。
在设计时,我创建了一个自定义的TPanel,因此可以将视觉组件放入其中,然后尝试从TPanel获取此控件并将它们存储在另一个组件中。
这是我的自定义面板
TDesignTimePanel = class(TPanel)
private
FPanel: TPanelDialogo;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
function GetChildOwner: TComponent; override;
end
GetChildren方法什么也不做,因为我不想以传统方式在DFM文件中编写此面板。方法GetChildOwner返回TPanelDialogo,我希望将视觉控件存储在该位置。
这是我要存储TDesignTimePanel控件的组件
TPanelDialogo = class(TComponent)
private
FDesignPanel: TDesignTimePanel;
procedure VolcarFrameEnLista();
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function CrearPanel(AOwner: TComponent): TPanel;
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
function GetChildOwner: TComponent; override;
end;
我以此方式创建自定义面板
function TPanelDialogo.CrearPanel(AOwner: TComponent): TPanel;
var
i: integer;
Componente : TControl;
begin
if FDesignPanel = nil then
begin
FDesignPanel := TDesignTimePanel.Create(self);
FDesignPanel.AsociarPanel( self );
end;
FDesignPanel.Name := Name + '_frame';
FDesignPanel.Left := FX;
// some other config
FDesignPanel.Parent := Owner as TWinControl;
FDesignPanel.Show;
Result := FDesignPanel;
end;
所以我的GetChildren方法执行以下操作,其中VolcarFrameEnLista是我从TDesignTimePanel对象获取控件并将它们存储在TPanelDialogo中的方法(FListaComponentes是TComponentList)
procedure TPanelDialogo.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
i: integer;
OwnedComponent: TComponent;
begin
if FDesignPanel <> nil then
begin
VolcarFrameEnLista();
if Root = Self then
for i := 0 to self.FListaComponentes.Count - 1 do
begin
OwnedComponent := FListaComponentes.Items[i];
Proc(OwnedComponent);
end;
end;
end;
procedure TPanelDialogo.VolcarFrameEnLista( );
var
i: integer;
Componente: TControl;
begin
for i := FDesignPanel.ControlCount - 1 downto 0 do
begin
Componente := FDesignPanel.Controls[i];
if Pos( self.Name + '_', Componente.Name ) = 0 then
begin
Componente.Name := self.Name + '_' + Componente.Name;
end;
Componente.Parent := nil;
if FListaComponentes.IndexOf(Componente) < 0 then
begin
FListaComponentes.Add( Componente );
end;
end;
end;
我希望我的DFM具有以下内容:
object Form1: TForm1
object PanelDialogo1: TPanelDialogo
Left = 712
// ...
object PanelDialogo1_Label1: TLabel
Left = 88
// ..
end
object PanelDialogo1_Label2: TLabel
Left = 40
// ..
end
end
end
但是我得到这样的东西
object Form1: TForm1
object PanelDialogo1: TPanelDialogo
Left = 712
// ...
end
object PanelDialogo1_Label1: TLabel
Left = 88
// ..
end
object PanelDialogo1_Label2: TLabel
Left = 40
// ..
end
end
我应该怎么做,TPanelDialogo会“拥有”在TDesignTimePanel上绘制的组件。
答案 0 :(得分:0)
我终于设法解决了我的问题。
我需要重写父对象上的GetChildren方法,以便将临时面板中的所有元素都放入TComponentList中。然后,我将此列表的每个元素写入DFM文件。
当读取DFM文件时,我在TPanelDialogo.Components属性中获得了此元素,但是由于Delphi环境中的重复控件,将这些元素存储在这里给我带来了麻烦。因此,在Loaded方法上,我将所有这些组件再次放入TComponentList。
这是代码
const expectedTypes = [
{
Id: 1,
Name: '-'
},
{
Id: 2,
Name: 'Some value here'
},
{
Id: 3,
Name: 'Some value here'
},
{
Id: 4,
Name: 'Some value here'
}
];
这是表格的DFM
type
TPanelDialogo = class;
// especialización de Frame para pruebas
TDesignTimePanel = class(TPanel)
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
end;
TPanelDialogo = class(TComponent)
private
FDesignPanel: TDesignTimePanel;
FGENPant: TGENPant;
FListaComponentes : TComponentList;
procedure CerrarPanel;
procedure VolcarFrameEnLista();
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function CrearPanel(AOwner: TComponent): TPanel;
procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
function GetChildOwner: TComponent; override;
procedure Loaded; override;
published
property ListaComponentes: TComponentList read FListaComponentes;
end;
procedure Register;
implementation
uses
ToolsApi,
SysUtils, Graphics,
Dialogs, StdCtrls,
ComponentesGEN;
{ TDesignTimePanel }
constructor TDesignTimePanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
destructor TDesignTimePanel.Destroy;
begin
inherited;
end;
procedure TDesignTimePanel.GetChildren(Proc: TGetChildProc; Root: TComponent);
begin
exit;
end;
constructor TPanelDialogo.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FListaComponentes := TComponentList.Create(True);
end;
destructor TPanelDialogo.Destroy;
begin
inherited Destroy;
end;
procedure TPanelDialogo.VolcarFrameEnLista( );
var
i: integer;
Componente: TControl;
OwnerName, ParentName: string;
begin
// recorrer el frame y rescatar sus componentes
if FDesignPanel = nil then
exit;
for i := FDesignPanel.ControlCount - 1 downto 0 do
begin
Componente := FDesignPanel.Controls[i];
if Componente.Owner <> nil then
OwnerName := Componente.Owner.Name;
if Componente.Parent <> nil then
ParentName := Componente.Parent.Name;
if Pos( self.Name + '_', Componente.Name ) = 0 then
begin
Componente.Name := self.Name + '_' + Componente.Name;
end;
if FListaComponentes.IndexOf(Componente) < 0 then
begin
FListaComponentes.Add( Componente );
end;
end;
end;
procedure TPanelDialogo.CerrarPanel;
begin
if FDesignPanel = nil then Exit;
FDesignPanel.Visible := false;
end;
function TPanelDialogo.GetChildOwner: TComponent;
begin
Result := self;
end;
procedure TPanelDialogo.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
i: integer;
OwnedComponent: TComponent;
begin
if FDesignPanel <> nil then
begin
VolcarFrameEnLista();
for i := 0 to self.FListaComponentes.Count - 1 do
begin
OwnedComponent := FListaComponentes.Items[i];
Proc(OwnedComponent);
end;
end;
end;
function TPanelDialogo.CrearPanel(AOwner: TComponent): TPanel;
var
i: integer;
Componente : TControl;
begin
if FDesignPanel = nil then
begin
FDesignPanel := TDesignTimePanel.Create(self);
FDesignPanel.AsociarPanel( self );
end;
FDesignPanel.Name := Name + '_frame';
// ...
try
for i := 0 to FListaComponentes.Count - 1 do
begin
Componente := FListaComponentes.Items[i] as TControl;
Componente.Parent := FDesignPanel;
end;
finally
FDesignPanel.Parent := Owner as TWinControl;
end;
FDesignPanel.Visible := true;
Result := FDesignPanel;
end;
procedure TPanelDialogo.Loaded;
var
i: integer;
OwnedComponent: TComponent;
begin
inherited;
for i := 0 to self.ComponentCount - 1 do
begin
OwnedComponent := self.Components[i];
self.FListaComponentes.Add(OwnedComponent);
end;
for i := self.ComponentCount - 1 downto 0 do
begin
OwnedComponent := self.Components[i];
self.RemoveComponent(OwnedComponent);
end;
self.FLoaded := true;
end;