• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi调用DLL中的窗体

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi线程Timer(TThreadTimer)发布时间:2022-07-18
下一篇:
Delphi中怎么结束线程(这个线程是定时执行的)(方案二)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap