------------------BPL中的类封装PAS文件bplclass--------------------
unit bplclass;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;//引用定义类、控件所需要的单元
type
Tbplfrom = class(TForm)//定义窗口类
Edit1: TEdit;//定义窗口类中的编辑框控件
Button1: TButton;//定义窗口类中的按钮控件
private
{ Private declarations }
public
{ Public declarations }
procedure mychangetext(YY: string); stdcall;//类中的函数调用,现在还有待研究
end;
except
函数名
implementation
procedure Tbplfrom.mychangetext(YY: string);
begin
showmessage('调用类函数成功');
end;
end.
--------------------------主程序--------------------------------
选在运行时创建包,否则将无法在调用BPL时,无法查找到类,只保留VCL,和BPL。
unit mainform;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, bplclass;//引用bplclass类,以便主程序可以获取bpl中定义的类封装的属性,控件等
type
Tmainfrm = class(TForm)
loadbpl: TButton;
changechild: TButton;
MAXchild: TButton;
minchild: TButton;
closechild: TButton;
Button1: TButton;
procedure loadbplClick(Sender: TObject);
procedure DoUnloadPackage(PKGHandle: HModule);
procedure MAXchildClick(Sender: TObject);
procedure minchildClick(Sender: TObject);
procedure closechildClick(Sender: TObject);
procedure changechildClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
BPLHandle: THandle;//定义BPL包的句柄参数
aForm: Tbplfrom;//安BPL包中的类来定义一个FORM变量
public
{ Public declarations }
end;
var
mainfrm: Tmainfrm;
implementation
USES System.Rtti, System.TypInfo, inifiles;//此处为BPL操作相关系统单元
{$R *.dfm}
procedure Tmainfrm.loadbplClick(Sender: TObject);
var
refclass: TFormClass; // 定义一个类引用变量.
dllFile: STRING;//BPL包的文件变量
begin
if aForm = NIL then //此处限制一个BPL只创建一个实例
BEGIN
// 调用包的传统方法1
dllFile := 'Package1.bpl';//定义BPL文件名
if BPLHandle = 0 then //判断包是否已经被加载
BPLHandle := LOADPACKAGE(dllFile); //如未被加载则执行加载操作
if BPLHandle = 0 then //判断是否加载成功
BEGIN
SHOWMESSAGE('装载包失败');
EXIT;
END;
if BPLHandle > 0 then //如果加载成功则执行以下操作
BEGIN
refclass := TFormClass(GETClass('Tbplfrom'));//查找BPL中的'Tbplfrom'类
if refclass = NIL then //如果找不到则提示
begin
SHOWMESSAGE('Tbplfrom没注册');
UNLOADPACKAGE(BPLHandle);
BPLHandle := 0;
EXIT;
end;
application.CreateForm(refclass, aForm); //创建类实例
WITH aForm DO //对该窗口类进行初始化设置
BEGIN
FormStyle := fsMDIChild;//定义为子窗口
// align := alClient;
// BorderStyle := bsNone;
// BorderIcons := [];
aForm.Caption := '222222222222222';
// aForm.WindowState := wsMAXImized;
aForm.Height := 300;
aForm.Width := 500;
aForm.Name := 'a' + inttostr(1 + self.MDIChildCount);
aForm.Caption := inttostr(1 + self.MDIChildCount);
Show;
END;
END;
END
ELSE
BEGIN
SHOWMESSAGE('窗口已经创建'); //如果窗口已经创建则提示
END;
end;
procedure Tmainfrm.MAXchildClick(Sender: TObject);
begin
aForm.WindowState := wsMaximized; //对子窗口操作,最大化
end;
procedure Tmainfrm.minchildClick(Sender: TObject);
begin
aForm.WindowState := wsminimized; //对子窗口操作,最小化
end;
procedure Tmainfrm.Button1Click(Sender: TObject);
begin
DoUnloadPackage(BPLHandle); //卸载BPL包
BPLHandle:=0;
end;
procedure Tmainfrm.changechildClick(Sender: TObject);
begin
aForm.Edit1.Text := 'aaa'; //设置子窗口中的控件参数,因为主程序已经引用了BPL的类定义单元bplclass,当实例创建时,即可对类中的属性,控件进行操作。
end;
procedure Tmainfrm.closechildClick(Sender: TObject);
begin
SHOWMESSAGE('当前子窗口数为' + inttostr(mainfrm.MDIChildCount));
aForm.Free; //关闭子窗口时需要的释放
aForm := nil;//关闭子窗口时需要的置空
SHOWMESSAGE('当前子窗口数为' + inttostr(mainfrm.MDIChildCount));
end;
procedure Tmainfrm.DoUnloadPackage(PKGHandle: HModule);//卸载包操作
var
i: Integer;
M: TMemoryBasicInformation;
begin
// 在你卸载一个包之前,记得销毁所有该包中的类对象,并反注册所有已注册的类。
for i := application.ComponentCount - 1 downto 0 do
begin
VirtualQuery(GETClass(application.Components[i].ClassName), M, Sizeof(M));
if (PKGHandle = 0) or (HModule(M.AllocationBase) = PKGHandle) then
application.Components[i].Free;
end;
UnregisterModuleClasses(PKGHandle);
if PKGHandle > 0 then
UNLOADPACKAGE(PKGHandle);
end;
initialization
RegisterClasses([Tmainfrm]); //程序进入时注册Tmainfrm类
finalization
UnRegisterClasses([Tmainfrm]);//程序退出时注销Tmainfrm类
UnRegisterClasses([Tbplfrom]);//程序退出时注销Tbplfrom类
end.