Delphi XE2 中从另一个包中的基本表单进行视觉继承,无需源代码

Mik*_*lov 5 forms delphi inheritance delphi-xe2

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

此类操作的目的是向使用后代包的工作人员隐藏基础和应用程序源。

基础包包含一个表单: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;
Run Code Online (Sandbox Code Playgroud)

后代包包含 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;
Run Code Online (Sandbox Code Playgroud)

以及 Descendant.dfm 的代码:

inherited FormDescendant: TFormDescendant
  Caption = 'FormDescendant'
end
Run Code Online (Sandbox Code Playgroud)

后代.dpr:

requires
  rtl,
  vcl,
  Base;

contains
  Descendant in 'Descendant.pas' {FormDescendant};
Run Code Online (Sandbox Code Playgroud)

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

但是,当我们尝试在 Delphi IDE 中打开 FormDescendant 时,它会崩溃并显示“创建表单时出错:未找到‘TFormBase’的祖先”。没错:Base.bpl 仅包含二进制代码,而 Delphi 不知道 TBaseForm 在设计时的外观。

我应该怎么做才能在Delphi中打开FormDescendant?

我已阅读如何在 Delphi 中使用或解决可视表单继承问题?注册自定义表单,以便我可以从多个项目继承它,而无需将表单复制到对象存储库文件夹 但这些建议没有帮助。 有没有办法在设计时打开 FormDescendant 而无需 TFormBase 源?


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

Ond*_*lle 1

您可以提供一个已删除(部分)实现代码的存根单元,仅 .dfm 和接口必须相同。这就是 Allen Bauer 在他的Opening Doors文章中所做的,该文章展示了如何实现可停靠 IDE 表单

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