一个调用DLL中的窗体实例
在MDI窗体类型的编程中,主窗体(fsMDIForm)怎样调用子窗体(fsMDIChild,此子窗体为DLL)。
以下是我的原代码:
==========================================================================================
DPR 单元代码
program Mdiform;
uses
Forms,
UMdiform in 'UMdiform.pas' {Mainform},
UDM in 'UDM.pas' {GlobalDM: TDataModule},
UFun in 'UFun.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TMainform, Mainform);
Application.Run;
end.
===============================================================================================
主窗体代码:
unit UMdiform;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Udm,StdCtrls;
type
TMainform = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
MyHandle:HWND;
{ Public declarations }
end;
var
Mainform: TMainform;
type
T_ShowTestMng=function (var adm:TMainform) : Boolean; StdCall;
implementation
{$R *.dfm}
procedure TMainform.Button1Click(Sender: TObject);
var
Lib_ :THandle;
_ShowTestMng :T_ShowTestMng;
begin
Lib_:=LoadLibrary(pchar('MdiChild.dll'));
try
@_ShowTestMng:=GetProcAddress(Lib_,'_ShowTestMng');
if not(@_ShowTestMng=nil) then
_ShowTestMng(Mainform);
finally
FreeLibrary(Lib_);
end;
end;
procedure TMainform.FormCreate(Sender: TObject);
begin
MyHandle:=Application.Handle;
end;
end.
==============================================================================================
子窗体DLL代码:
library MdiChild;
uses
ShareMem,
UMdiform, //此单元为父窗体单元,在顶目设置中我已经设置了搜索此单元在路径。
Forms,
SysUtils,
Classes,
UChild in 'UChild.pas' {FrmChild};//FrmChild子窗体的FormStyle属性为FsMDIChild
{$R *.res}
function _ShowTestMng(var adm:TMainform) : Boolean; StdCall;
begin
result:=true;
Application.Handle:=adm.MyHandle;
Application.CreateForm(TFrmChild,FrmChild); //程序就出错在此:出错原因是:Cannot create form. No MDI Forms are currently active.
FrmChild.Show;
end;
exports
_ShowTestMng;
end.