Delphi 调用DLL中的窗体

本文介绍了一种在DLL中加载并显示窗体的方法。通过使用Delphi提供的API,可以实现在DLL被调用时弹出指定窗体的功能。这种方法适用于需要将用户界面组件封装为动态链接库的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

library Project1;

uses
SysUtils,
   Classes,Forms,windows,dialogs,
  Unit1 
in 'Unit1.pas' {Form1};

{$R *.res}
function showform(formname:string):boolean;stdcall;
var
  TheClass: TPersistentClass;
  aForm: TForm;
begin
result:
=false;
{如果您的Dll中有很多FORM,请在这儿注册哦
RegisterClasses([TForm1,TForm2,TForm3,...]);
}
RegisterClasses([TForm1]);
TheClass :
= GetClass('T' + FormName);
if (TheClass = nil) then   exit;
if TheClass.InheritsFrom(TForm)  then
begin
    aForm :
= Tform(TheClass.Create).Create(nil);
    
try
      aForm.ShowModal;
      result:
=true;
    
finally
      FreeAndNil(aForm);
    
end;

end;
end;

exports
showform;
begin
end.


   
  
....


procedure  RunDllForm(const DllFileName,DllFormName:String;const methodName:string);
type
TRunForm
=function(formname:string):boolean;stdcall;
var
  RunForm: TRunForm;
  GetDllHWND: HWND;
begin
  GetDllHWND :
= LoadLibrary(PChar(DllFileName));
  
try
    
if GetDllHWND < 32 then
    
begin
      MessageBox(
0, Pchar('没有找到'+DllFileName+'DLL文件!'),'加载DLL失败', MB_OK);
      Exit;
    
end;
    @RunForm :
= GetProcAddress(GetDllHWND,pchar(methodName));
    
if @RunForm <> nil then
       
try
         RunForm(DllFormName);
       
except
         
raise Exception.Create('对不起,找不到T' + DllFormName+ '窗体!');
       
end
     
else
     
raise Exception.Create('无效的方法名调用');
  
finally
    FreeLibrary(GetDllHWND);
  
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RunDllForm(
'project1.dll','form1','showform');
end;

转载于:https://www.cnblogs.com/Values/p/3232262.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值