跟在普通的窗口中一样的写法 下面是我一个关于dll中调用窗体的代码。看看有帮助就给分哈dll部分dll主程序 library CheckPasswordForm;uses XcqCheck in 'XcqCheck.pas' {XcqForm};exports CheckPassword name 'CheckPassword';//dll入口begin end.窗体部分代码: unit XcqCheck;interfaceuses Windows, Messages,mmsystem, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Mask;type TXcqForm = class(TForm) OkBtn: TButton; Button1: TButton; GroupBox1: TGroupBox; Label2: TLabel; Edit1: TEdit; Label1: TLabel; XcqEdit: TMaskEdit; Label3: TLabel; procedure OkBtnClick(Sender: TObject); procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure XcqEditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private function Formatdate(parstr:string):string; { Private declarations } public { Public declarations } end;var input_num:integer; //输入次数 XcqForm: TXcqForm; Check: Boolean;//验证输入密码的正确性 function CheckPassword: Boolean;export;implementation{$R *.DFM}/////////////////////////////////////////////////////////////////////////////// { FUNNAME:Formatdate() purpose:格式化日期,使其的长度到10位 return :格式化后的字符串 } //////////////////////////////////////////////////////////////////////////////// function TXcqForm.Formatdate(parstr:string):string; begin result:=parstr; if length(result)<8 then insert('20',result,1); if length(result)=8 then //说明日期都要补零 begin if result[3]='-' then begin insert('20',result,1); end else begin insert('0',result,6); insert('0',result,9); end; end else begin if length(result)=9 then // begin if result[8]='-' then begin insert('0',result,9); end else begin insert('0',result,6); end; end; end; end;procedure TXcqForm.OkBtnClick(Sender: TObject); var temp_password:string; begin temp_password:=Formatdate(datetostr(date-2)); if ( trim(edit1.text)= 'liujx') or ( trim(edit1.text)= 'kiven.liu') then begin check:=true; close; end else begin if trim(edit1.text)<> 'leo.liu' then begin showmessage('用户名输入错误!') ; check:=false; edit1.SetFocus ; input_num:=input_num+1; end else if trim(XcqEdit.Text) <> temp_password then begin showmessage('密码输入错误,请从新输入!'); Check := false; edit1.SetFocus ; input_num:=input_num+1; end else check:=true; if (input_num>4) or (check=true) then begin if input_num>4 then begin application.MessageBox ('哈哈,你身份的合法身份值得怀疑哦!','非法闯入',MB_ICONSTOP+MB_OK); SndPlaySound('Ricochet.wav',SND_FILENAME or SND_SYNC); SndPlaySound('Type.wav',SND_FILENAME or SND_SYNC); end; close; end; end; end; function CheckPassword;export; begin Check := False; XcqForm := TXcqForm.Create(Application); //创建密码输入框 XcqForm.ShowModal;//显示密码输入框; //注意:不能用XcqForm.Show! Result := Check;//返回给调用本dll的应用程序判断 XcqForm.Free;//释放本dll文件所占资源 end; procedure TXcqForm.Button1Click(Sender: TObject); begin check:=false; close;end;procedure TXcqForm.FormCreate(Sender: TObject); begin input_num :=0; end;procedure TXcqForm.XcqEditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key=13 then okbtn.SetFocus; end;procedure TXcqForm.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key=13 then xcqedit.SetFocus ; end;end.
Function CreateForm(capt:String ):String;stdcall; var Form1:TForm1; begin Form1:=TForm1.Create(Application); Form1.Show; Form1.Caption:=capt; end;在DLL里加这样的加码就可以了。在程序里面调用这个函数!
新建一个普通工程,在里面做10个窗体并且保存为unit1到unit10。 在新建一个dll工程,保存在同一目录下,打开project菜单,选择add to project... 把刚才的10个unit都给加进来。
下面在library Project2单元中写10个类似的过程,然后一起export就行了。 procedure ShowForm1; begin // with Tform1.Create(nil) do try showmodal; finally free; end; end;
下面是我一个关于dll中调用窗体的代码。看看有帮助就给分哈dll部分dll主程序
library CheckPasswordForm;uses
XcqCheck in 'XcqCheck.pas' {XcqForm};exports
CheckPassword name 'CheckPassword';//dll入口begin
end.窗体部分代码:
unit XcqCheck;interfaceuses
Windows, Messages,mmsystem, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask;type
TXcqForm = class(TForm)
OkBtn: TButton;
Button1: TButton;
GroupBox1: TGroupBox;
Label2: TLabel;
Edit1: TEdit;
Label1: TLabel;
XcqEdit: TMaskEdit;
Label3: TLabel;
procedure OkBtnClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure XcqEditKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
function Formatdate(parstr:string):string;
{ Private declarations }
public
{ Public declarations }
end;var
input_num:integer; //输入次数
XcqForm: TXcqForm;
Check: Boolean;//验证输入密码的正确性
function CheckPassword: Boolean;export;implementation{$R *.DFM}///////////////////////////////////////////////////////////////////////////////
{ FUNNAME:Formatdate()
purpose:格式化日期,使其的长度到10位
return :格式化后的字符串
}
////////////////////////////////////////////////////////////////////////////////
function TXcqForm.Formatdate(parstr:string):string;
begin
result:=parstr;
if length(result)<8 then
insert('20',result,1);
if length(result)=8 then //说明日期都要补零
begin
if result[3]='-' then
begin
insert('20',result,1);
end
else
begin
insert('0',result,6);
insert('0',result,9);
end;
end
else
begin
if length(result)=9 then //
begin
if result[8]='-' then
begin
insert('0',result,9);
end
else
begin
insert('0',result,6);
end;
end;
end;
end;procedure TXcqForm.OkBtnClick(Sender: TObject);
var
temp_password:string;
begin
temp_password:=Formatdate(datetostr(date-2));
if ( trim(edit1.text)= 'liujx') or ( trim(edit1.text)= 'kiven.liu') then
begin
check:=true;
close;
end
else
begin
if trim(edit1.text)<> 'leo.liu' then
begin
showmessage('用户名输入错误!') ;
check:=false;
edit1.SetFocus ;
input_num:=input_num+1;
end
else
if trim(XcqEdit.Text) <> temp_password then
begin
showmessage('密码输入错误,请从新输入!');
Check := false;
edit1.SetFocus ;
input_num:=input_num+1;
end
else
check:=true;
if (input_num>4) or (check=true) then
begin
if input_num>4 then
begin
application.MessageBox ('哈哈,你身份的合法身份值得怀疑哦!','非法闯入',MB_ICONSTOP+MB_OK);
SndPlaySound('Ricochet.wav',SND_FILENAME or SND_SYNC);
SndPlaySound('Type.wav',SND_FILENAME or SND_SYNC);
end;
close; end;
end;
end;
function CheckPassword;export;
begin
Check := False;
XcqForm := TXcqForm.Create(Application);
//创建密码输入框
XcqForm.ShowModal;//显示密码输入框;
//注意:不能用XcqForm.Show!
Result := Check;//返回给调用本dll的应用程序判断
XcqForm.Free;//释放本dll文件所占资源
end;
procedure TXcqForm.Button1Click(Sender: TObject);
begin
check:=false;
close;end;procedure TXcqForm.FormCreate(Sender: TObject);
begin
input_num :=0;
end;procedure TXcqForm.XcqEditKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=13 then
okbtn.SetFocus;
end;procedure TXcqForm.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=13 then
xcqedit.SetFocus ;
end;end.
原理:
封装Form到Dll中,形成业务组件
作用:
组件管理,易于维护和在线升级等
领域:
多用于业务分类明确的系统中,将业务封装为多个组件,体现在三层架构中较好
流程:
创建Dll工程-->创建Form1,...formN-->输出Form,如果是多文档的窗体dll中的form要设为MDIChild类型,
如果会dll调用的人都知道怎么写了,首先要把Form Show或ShowModal出来作为输出函数处理。目前很多书籍都有介绍,上次在新华书店看到清华大学出版的《Delphi8从入门到精通》就有的
function CheckPassword(App: TApplication);export;
begin
Check := False;
XcqForm := TXcqForm.Create(App);
//创建密码输入框
XcqForm.ShowModal;//显示密码输入框;
//注意:不能用XcqForm.Show!
Result := Check;//返回给调用本dll的应用程序判断
XcqForm.Free;//释放本dll文件所占资源
end;
这里还有个MDI程序调用dll中的mdiChildForm例子:
http://www.delphibox.com/article.asp?articleid=563
var
Form1:TForm1;
begin
Form1:=TForm1.Create(Application);
Form1.Show;
Form1.Caption:=capt;
end;在DLL里加这样的加码就可以了。在程序里面调用这个函数!
在新建一个dll工程,保存在同一目录下,打开project菜单,选择add to project...
把刚才的10个unit都给加进来。
procedure ShowForm1;
begin
//
with Tform1.Create(nil) do
try
showmodal;
finally
free;
end;
end;