在Delphi XE2中没有源代码的另一个包中的基本表单的可视继承

时间:2013-01-08 18:56:39

标签: forms delphi inheritance delphi-xe2

有一个情况:两个包:“Base”和“Descendant”以及一个应用程序“Example”。 Base包和Example应用程序可以在一个项目组中,但Descendant包必须在其他项目组中,没有Base和Example的任何源代码。

此类操作的目的是隐藏将使用Descendant包的工作人员的Base和Application源。

Base包包含一个表单:TFormBase,包含一些组件和一些代码。我构建它并获得一些二进制文件:bpl,dcp等...

type
  TFormBase = class(TForm)
    Panel1: TPanel;
    BOk: TButton;
    BCancel: TButton;
    procedure BOkClick(Sender: TObject);
    procedure BCancelClick(Sender: TObject);
  private
  protected
    function GetOkButtonCaption: string; virtual;
    function GetCancelButtonCaption: string; virtual;
  public
  end;

implementation

{$R *.dfm}


procedure TFormBase.BCancelClick(Sender: TObject);
begin
  ShowMessage('"' + GetCancelButtonCaption + '" button has been pressed');
end;

procedure TFormBase.BOkClick(Sender: TObject);
begin
  ShowMessage('"' + GetOkButtonCaption + '" button has been pressed');
end;

function TFormBase.GetCancelButtonCaption: string;
begin
  Result := 'Cancel';
end;

function TFormBase.GetOkButtonCaption: string;
begin
  Result := 'Ok';
end;

Descendant包中包含TFormDescendant = class(TFormBase)

type
  TFormDescendant = class(TFormBase)
  private
  protected
    function GetOkButtonCaption: string; override;
    function GetCancelButtonCaption: string; override;
  public
  end;

implementation

{$R *.dfm}


function TFormDescendant.GetCancelButtonCaption: string;
begin
  Result := 'Descendant Cancel';
end;

function TFormDescendant.GetOkButtonCaption: string;
begin
  Result := 'Descendant Ok';
end;

Descendant.dfm的代码:

inherited FormDescendant: TFormDescendant
  Caption = 'FormDescendant'
end

Descendant.dpr:

requires
  rtl,
  vcl,
  Base;

contains
  Descendant in 'Descendant.pas' {FormDescendant};

创建FormDescendant时,它应该看起来像FormBase,因为它只是从它继承而来。我们可以在这个FormDescendant上添加一些其他组件来保存FormBase外观。

但是当我们尝试在Delphi IDE中打开FormDescendant时,它崩溃了“错误创建表单:未找到'TFormBase'的祖先。” 这是正确的:Base.bpl只包含二进制代码,而Delphi不知道TBaseForm在设计时的外观。

如何在Delphi中打开FormDescendant?

我看过How do I use or resolve issues with visual form inheritance in Delphi?Register custom form so I can inherit from it from multiple projects, without copying the form to the Object Repository folder 但那些建议没有帮助。 有没有办法在没有TFormBase源的情况下在设计时打开FormDescendant?


以下是实验示例的项目:http://yadi.sk/d/IHT9I4pm1iSOn

1 个答案:

答案 0 :(得分:1)

您可以提供一个存根单元(某些)实现代码被剥离,只有.dfm和接口必须相同。这就是Allen Bauer为他的Opening Doors文章所做的工作,该文章展示了如何实施dockable IDE forms

然后,您的开发人员需要先打开存根表单元,然后才能打开后代表单。