在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.