电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> delphi技术>>调用DLL文件中的窗体:

调用DLL文件中的窗体

来源:www.cncfan.com | 2006-1-16 | (有2623人读过)

关键词:DLL FORM 窗体
调用DLL文件中的FORM,具体实现过程如下:



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;


delphi技术热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号