dll窗体的创建与调用

建立DLL项目:

library ShowDllForm;
....
Uses ...
Exports //输出引用
ShowDllModalForm, //显示模态窗体
ShowDllForm; //显示非模态窗体
begin
end.

加入DLL窗体:

Unit DllFrm; //DllFrm表单源码
interface
...
...
private
...
public
...
end.
//声明过程
Procedure ShowDllModalForm(aHandle:THandle);stdcall;
Procedure ShowDllForm(aHandle:THandle);stdcall;
......
implementation
....
procedure ShowDllModalForm(aHandle:THandle);
begin
   application.handle:=ahandle;
   with TDllFrm.create(application) do
   begin
        try
           showmodal;
        finally
           free;
        end;
   end;
end;
Procedure ShowDllForm(aHandle:THandle);
begin
   application.handle:=ahandle;
   with tdllfrm.create(application) do
         show;
end;
.....
end.

静态调用:

//声明过程:
Unit ...
Interface ...
uses ...
   //声明静态链接DLL引出的过程
   procedure ShowDllModalForm(aHandle:tHandle);stdcall external '.\ShowDllForm.dll';
   procedure ShowDllForm(aHandle:thandle);stdcall external '.\showdllform.dll';
type...
.....
procedure TForm1.Button1Click(Sender: TObject);
begin
//按声明的函数调用DLL引出的过程,参数是当前应用程序的句柄
showdllmodalform(application.Handle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//按声明的函数调用DLL引出的过程,参数是当前应用程序的句柄
showdllform(application.Handle);
end;

end.

动态调用:

unit ...
interface ...
uses
    ....
type
    .....
private
    DllHandle:THandle; //DLL句柄

    TShowDllForm=procedure(aHandle:THandle);stdcall; //类定义,DLL引出的过程类型

    ....
//表单建立时装入DLL文件
procedure TForm1.FormCreate(sender:tobject);
begin
   DllHandle:=LoadLibrary('.\dll\DllShowForm.dll');
   if DllHandle=0 then
      showmessage('无法装入DLL文件.');
end;
//表单释放时,同时释放Dll
procedure TForm1.FormDestroy(sender:Tobject);
begin
   freelibrary(dllhandle);
end;

//显示模态窗体
var
   ShowModalForm:TShowDllForm;
begin
   @ShowModalForm:=GetProcAddress(dllhandle,'ShowDllModalForm');
   showModalForm(application.handle);
end;

//显示非模态窗体
var
   showFrm:TshowDllForm;
begin
   @showfrm:=GetProcAddress(dllHandle,'ShowDllForm'); //注意DLL内函数名的大小写
   showfrm(application.handle);
end;

每天早上敲醒自己的不是闹钟,是夢想!
原文地址:https://www.cnblogs.com/yplong/p/2344811.html