1. 在DLL中创建模态和非模态窗口并传递参数
//////////////// DLL /////////////////////////////////
library MyForm;
uses
SysUtils,
Classes,
Forms,
controls,
Dialogs,
MyModal in 'MyModal.pas' {MyMForm},
ModLess in 'ModLess.pas' {MyModless};
{$R *.RES}
type THandle = Integer;
procedure showMyForm(p
char;handle:THandle);stdcall;
var
myForm:TMyMForm;
begin
Application.Handle:=handle;
myForm:=TMyMForm.MyCreate(p) ;
myForm.showmodal;
myForm.free;
end;
procedure showMyModLess(p
char;handle:THandle);stdcall;
begin
if not assigned(noMod) then
begin
Application.Handle:=handle;
NoMod:=TMyModLess.MyCreate(p) ;
end;
NoMod.show;
end;
exports showMyForm,showMyModless;
begin
FreeAndNil(NoMod);
end.
///////////////////////////// 模态窗口 ////////////////////////////////////////////
unit MyModal;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMyMForm = class(TForm)
Edit1: TEdit;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
FP
ointer;
{ Private declarations }
public
{ Public declarations }
constructor MyCreate(s
char);
end;
implementation
{$R *.DFM}
{ TMyMForm }
constructor TMyMForm.MyCreate(s: pchar);
begin
inherited Create(nil);
fp:=s;
Edit1.Text:=ShortString(fp^);
end;
procedure TMyMForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ShortString(fp^):=Edit1.Text;
end;
procedure TMyMForm.FormCreate(Sender: TObject);
begin
icon.LoadFromFile ('winupd.ico');
end;
end.
////////////////////////////////// 非模态窗口 //////////////////////////////
unit ModLess;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMyModless = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
FP
ointer;
public
{ Public declarations }
constructor MyCreate(s
char);
end;
var
NoMod:TMyModLess;
implementation
{$R *.DFM}
{ TMyModless }
constructor TMyModless.MyCreate(s: pchar);
begin
inherited Create(nil);
fp:=s;
Edit1.Text:=ShortString(fp^);
end;
procedure TMyModless.FormCreate(Sender: TObject);
begin
icon.LoadFromFile ('winupd.ico');
end;
procedure TMyModless.Button1Click(Sender: TObject);
begin
ShortString(fp^):=Edit1.Text;
end;
procedure TMyModless.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=caFree ;
NoMod:=nil;
end;
end.
//////////////////////// 调用程序 /////////////////////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure showMyForm(p
char;handle:THandle);stdcall;external 'MyForm.dll';
procedure showMyModLess(p
char;handle:THandle);stdcall;external 'MyForm.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
s:ShortString;
begin
s:=Edit1.text;
showmyform(@s,handle);
Edit1.text:=s;
end;
var
s:ShortString='ABCDEF';
procedure TForm1.Button2Click(Sender: TObject);
begin
showmyModLess(@s,handle);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
showmessage(s);
end;
end.