【发布时间】:2011-12-18 23:37:47
【问题描述】:
我有一个应用程序可以加载一个简单表单中的 BPL。
此表单是主应用程序的可选选项。
BPL 加载正确,表单显示正确,但是我不知道如何访问 bpl 中表单的公共方法和属性。
谁能提供一个简单的例子?
我的代码:
// Load the BPL on aplication Load
LoadPackage( 'About.bpl' );
// CAll for TForm1 inside the About.BPL
var
AClass: TClass;
AForm: TForm;
begin
AClass := GetClass('TForm1');
if AClass <> nil then
begin
Application.CreateForm(TComponentClass(AClass), AForm);
AForm.Show;
end;
// The unit TForm1 inside the BPL package
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
PublicMthd;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Procedure TForm1.PublicMthd;
Begin
ShowMessage('Inside call');
End;
initialization
RegisterClass(TForm1);
finalization
UnRegisterClass(TForm1);
end.
如何在 Tform1 中访问“PublicMthd”?
【问题讨论】:
-
向我们展示您的代码是什么样的。加载 BPL 文件的方法有很多种,答案取决于您的操作方式。
-
这个 BPL 是你写的吗?为什么不导出一个叫GetMainForm的函数,然后调用
function GetMainForm:TForm就可以访问了?您是否有理由不能使用常识方法?一旦你知道类并且可以枚举它们,甚至可能创建它们的实例,你打算做什么?您是否有理由没有指定适合您的问题域的 IPluginInterface,而不是直接从您的主应用程序转到底层类类型? -
您的可执行文件对 bpl 中的“TForm1”一无所知,只知道它是(或源自)TForm。因此,您只能访问 TForm 的方法和属性。不管你喜不喜欢,弗朗索瓦的答案是正确的。