在Delphi组件之间移动控件 [英] Moving controls between Delphi components

查看:130
本文介绍了在Delphi组件之间移动控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以容纳一些视觉组件的Delphi非视觉组件。

I´m trying to create a Delphi non-visual component that can hold some visual components.

在设计时,我创建了一个自定义的TPanel,因此我可以放置我的视觉组件,然后尝试从TPanel获取此控件并将它们存储在另一个组件中。

In design time I create a custom TPanel, so I can put my visual components in it and then I try to get this controls from the TPanel and store them in another component.

这是我的自定义面板

  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,我要将视觉控件存储在其中。

The method GetChildren does nothing, as I don't want to write this panel in a traditional way in the DFM file. The method GetChildOwner returns the TPanelDialogo where I want the visual controls to be stored.

这是我要从TDesignTimePanel

And this is the component where I want to store the controls from the 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;

我以此方式创建自定义面板

I create the custom panel this way

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)

So my GetChildren method does the following, where VolcarFrameEnLista is the method where I take the controls from the TDesignTimePanel object and stores them in the TPanelDialogo (FListaComponentes is a 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具有以下内容:

I want my DFM to have something like this:

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需要

What should I do so the TPanelDialogo takes "ownership" of the components drawn on the TDesignTimePanel.

推荐答案

I final ly设法解决了我的问题。

I finally managed to solve my problem.

我需要重写父对象上的GetChildren方法,以便将临时面板中的所有元素都放入TComponentList中。然后,我将此列表的每个元素写入DFM文件。

What I needed was to overwrite the GetChildren method on my parent object so I can get all the elements in the temporary panel into a TComponentList. Then I write each element of this list into the DFM file.

在读取DFM文件时,我会在TPanelDialogo.Components属性中获取此元素,但是将其存储在此处我很麻烦,因为来自Delphi环境的重复控件。因此,在Loaded方法上,我再次将所有这些组件放到TComponentList中。

When reading the DFM file I get this elements in the TPanelDialogo.Components property, but storing this elements here got me trouble because of the duplicate control from Delphi environment. So on the Loaded method I put all these components into the TComponentList again.

这里是代码

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;

这是在设计时显示的内容:

This is what is shown at Design Time:

这是以下形式的DFM

And this is the DFM of the form

object Form1: TForm1
  ...
  object PanelDialogo1: TPanelDialogo
    ...
    object PanelDialogo1_Label2: TLabel
      Caption = 'Another label right here'
    end
    object PanelDialogo1_Label1: TLabel
      Caption = 'A label in the top of the panel'
    end
    object PanelDialogo1_Edit1: TEdit
      Text = 'Write something here...'
    end
    object PanelDialogo1_Panel1: TPanel
      object PanelDialogo1_Button1: TButton
        Caption = 'OK'
      end
    end
    object PanelDialogo1_Label3: TLabel
      Caption = 'Some label just here'
    end
  end
end

这篇关于在Delphi组件之间移动控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆