怎么把窗体编译成dll,怎么调用窗体dll?

yball 2002-07-25 09:05:55
不如unit1.pas是主窗体,要把unit2.pas放入unit2.dll,并且在unit1.pas可以调用unit2?
...全文
44 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjl 2002-07-25
  • 打赏
  • 举报
回复
标记一下
stiwin 2002-07-25
  • 打赏
  • 举报
回复
在Dll文件中uses你的窗体
yball 2002-07-25
  • 打赏
  • 举报
回复
ok,结帖加分!150+50
careerist 2002-07-25
  • 打赏
  • 举报
回复
DLL文件:
library Main;

uses
Main in 'Main.pas' {fmMain};

exports
ShowForm;

{$R *.res}

begin
end.

PAS文件:
unit Main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, hbDialog, DB, DBClient, MConnect, StdCtrls, Buttons, ActiveX;
type
...
var
...
procedure ShowForm(可以加些参数); stdcall;

implementation

procedure ShowForm();
begin
try
ShowModal;
finally
Free;
end;
end;


调用DLL中的函数:

procedure TForm2.Button1Click(Sender: TObject);
var
aHandle: THandle;
ShowForm: procedure(); stdcall;
begin
aHandle := LoadLibrary(Main.DLL');
if aHandle <> 0 then
@ShowForm := GetProcAddress(aHandle,'ShowForm');
if @ShowForm <> nil then
begin
try
ShowForm();
finally
FreeLibrary(aHandle);
end;
end;
end;
gmc007 2002-07-25
  • 打赏
  • 举报
回复
如果你了解一点COM,我想这个问题很简单。
toplor 2002-07-25
  • 打赏
  • 举报
回复
首先将Form2编进DLLPro.dll中,怎么编@##¥¥%^&J*,代码如下:

////////////////////////DLLPro.dpr/////////////////////////////////
library DLLPro;

{ 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' {Form2};

{$R *.res}

exports
myShowForm2,
myCloseForm2;

begin
end.

///////////////////////Unit2.pas///////////////////////////////////
unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TForm2 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;

{var
Form2: TForm2;}

function myShowForm2(AHandle:THandle):longint;stdcall;
procedure myCloseForm2(AFormRef:longint);stdcall;

implementation

{$R *.dfm}

function myShowForm2(Ahandle:Thandle):longint;
var
form2:Tform2;
begin
application.Handle:=AHandle;
form2:=tform2.Create(application);
form2.Show;
result:=longint(form2);
end;

procedure myCloseForm2(AformRef:longint);
begin
if AFormRef>0then
Tform2(AformRef).Release;
end;

end.


编绎成功后,在任何工程中都可以调用DllPro.dll,显示非模式的Form2,如下:
////////////////////////Project1.dpr/////////////////////////////
program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

///////////////////////////Unit1.pas///////////////////////////////
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
FormRef:longint;

function myShowForm2(AHandle:THandle):longint;stdcall;
procedure myCloseForm2(AFormRef:longint);stdcall;

implementation

function myShowForm2;external 'DLLPro.dll';
procedure myCloseForm2;external 'DllPro.dll';

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
FormRef:=myShowForm2(application.Handle);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
myCloseForm2(FormRef);
end;

end.


-------------------------------------------------------------------
风过西窗客渡舟船无觅处
年年一川新草遥看却似旧
yball 2002-07-25
  • 打赏
  • 举报
回复
给个例子!谢谢!如果搞定的话,再加100分!
careerist 2002-07-25
  • 打赏
  • 举报
回复
先把unit2.pas加入到unit2.dll项目中,要在unit2.pas中写一段类似于ShowForm的接口函数.
再在unit1.pas中调用unit2.pas中的接口函数.

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧