///////////////// DLL /////////////////////
library FormDll;
uses
SysUtils,
windows,
Classes,
forms,
MyForm in 'MyForm.pas' {ChildForm};
{$R *.RES}
var
DLLApp: TApplication;
OldSc:TScreen;
procedure DLLUnloadProc(Reason : Integer);
begin
if Reason = DLL_PROCESS_DETACH then begin
Application := DLLApp;
Screen:=OldSc;
end;
end;
function ShowForm(App:TApplication;sc:TScreen): integer;
var
i:integer;
begin
application:=App;
Screen:=sc;
for i:=0 to N do
if ch=nil then
begin
ch:=TChildForm.Create(nil);
// ch.parent:=application.mainform;
ch.tag:=i;
break;
end;
Result:=0;
end;
exports ShowForm;
var
i:integer;
begin
for i:=0 to N do ch:=nil;
DLLApp := Application;
OldSc:=Screen;
DLLProc := @DLLUnloadProc;
end.
/////////// mdiform unit //////////////////
unit MyForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TChildForm = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const N=100;
var
ch:array[0..N] of TChildForm;
implementation
{$R *.DFM}
procedure TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
ch[tag]:=nil;
end;
procedure TChildForm.FormCreate(Sender: TObject);
begin
inherited;
FormStyle := fsMDIChild;
end;
end.
////////////// 调用程序 //////////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// 创建动态按钮,第一排一个,第十排十个
var
b:array of array of TButton;
procedure TForm1.FormCreate(Sender: TObject);
var
i,j:integer;
begin
SetLength(b,10);
for i:=low(b) to high(b) do
SetLength(b,i+1);
for i:=low(b) to high(b) do
for j:=low(b) to high(b) do
begin
b[i,j]:=TButton.Create(self);
with b[i,j] do
begin
parent:=self;
left:=j*30;
top:=i*30;
width:=20;
height:=20;
visible:=true;
end;
end;
end;
function ShowForm(App:TApplication;sc:TScreen): integer;external 'formdll.dll'
procedure TForm1.Button1Click(Sender: TObject);
begin
showform(application,screen);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
showmessage(inttostr(MDIChildCount));
end;
end.