将窗体资源分装到DLL中并且调用
用Delphi生成DLL并封装窗体的示例
调用Dll里面的窗体
用Delphi生成DLL并封装窗体的示例
调用Dll里面的窗体
DLL文件
library Project2;
{ Important note about DLL
memory management: ShareMem must be the
first unit in your
library's USES clause AND your project's (select
Project-View
Source) USES clause if your DLL exports any procedures
or
functions that pass strings as parameters or function results.
This
applies to all strings passed to and from your DLL--even
those that
are nested in records and classes. ShareMem is the
interface unit to
the BORLNDMM.DLL shared memory manager, which
must be deployed along
with your DLL. To avoid using
BORLNDMM.DLL, pass string information
using PChar or ShortString
parameters. }
uses
SysUtils,
Classes,
Unit2
in 'Unit2.pas'
{Form1};
{$R
*.res}
exports
Dll_Showform,
Dll_CreateForm,
DLL_GetValue,
DLL_SetTitle;
begin
end.
unit Unit2;
interface
uses
Windows, Messages, SysUtils,
Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,
Buttons;
type
TForm1
= class(TForm)
Memo1:
TMemo;
BitBtn1: TBitBtn;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1:
TForm1;
//Ready for DLl Export
list
procedure
Dll_Showform();stdcall;
procedure
Dll_Createform(Left,Top,Width,Height:Integer);stdcall;
function DLL_GetValue():string;stdcall;
procedure DLL_SetTitle(text:string); stdcall;
implementation
{$R
*.dfm}
//无参数
procedure Dll_Showform();stdcall;
var frm:TForm1;
begin
frm:=TForm1.Create(Application);
frm.Position:=poMainFormCenter;
frm.ShowModal;
end;
//带参数
procedure
Dll_Createform(Left,Top,Width,Height:Integer);stdcall;
var frm:TForm1;
begin
frm:=TForm1.Create(Application);
frm.Left:=Left;
frm.Top:=Top;
frm.Width:=Width;
frm.Height:=Height;
frm.BorderStyle:=bsNone;
frm.ShowModal;
end;
//获得返回值
function
DLL_GetValue():string;stdcall;
var
frm:TForm1;
begin
Result:='';
frm:=TForm1.Create(Application);
frm.Position:=poMainFormCenter;
if frm.ShowModal = mrok
then
Result:=frm.Memo1.Lines.Text;
end;
//回调
procedure DLL_SetTitle(text:string); stdcall;
var frm:TForm1;
begin
frm:=TForm1.Create(Application);
frm.Position:=poMainFormCenter;
frm.Caption:=text;
frm.ShowModal;
end;
end.
调用Dll
unit Unit1;
interface
uses
Windows, Messages, SysUtils,
Variants, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls;
//-------------------------------------------------------------------------------
procedure Dll_Showform();stdcall;external 'project2.dll'; //1无参数
直接调用
procedure
Dll_Createform(Left,Top,Width,Height:Integer);stdcall;external 'project2.dll';//2带参数了
function DLL_GetValue():string;stdcall;external 'project2.dll';//3获得返回值
//-------------------------------------------------------------------------------
type
TfrmMain1 = class(TForm)
Button1:
TButton;
Button2:
TButton;
Button3:
TButton;
Memo1:
TMemo;
Button4:
TButton;
procedure
Button1Click(Sender: TObject);
procedure Button2Click(Sender:
TObject);
procedure
Button3Click(Sender: TObject);
procedure Button4Click(Sender:
TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain1: TfrmMain1;
implementation
{$R
*.dfm}
//------------------------------------------------------------------------------
procedure TfrmMain1.Button1Click(Sender:
TObject);
begin
Dll_Showform;
end;
//------------------------------------------------------------------------------
procedure TfrmMain1.Button2Click(Sender:
TObject);
var
x,y:Integer;
begin
x:=frmMain1.left+Button2.left+8; //Left
y:=frmMain1.Top+Button2.Top +Button2.Height+28; //Top
Dll_Createform(x,y,400,400);
// Dll_Createform(0,0,100,200);
end;
//------------------------------------------------------------------------------
procedure TfrmMain1.Button3Click(Sender:
TObject);
begin
Memo1.Text:=DLL_GetValue;
end;
//------------------------------------------------------------------------------
type
TSetCaption = procedure(text:string); stdcall;//4回调
procedure
TfrmMain1.Button4Click(Sender: TObject);
var
pSetTitle:
TSetCaption;
h:THandle;
begin
h:=LoadLibrary(PChar('project2.dll'));
pSetTitle
:= TSetCaption( GetProcAddress(h, PChar('DLL_SetTitle')) );
if h = 0 then
Exit;
if
Assigned(pSetTitle) then
pSetTitle('Hello
World!');
FreeLibrary(h);
end;
end.