怎么把窗体编译成dll,怎么调用窗体dll?
不如unit1.pas是主窗体,要把unit2.pas放入unit2.dll,并且在unit1.pas可以调用unit2? 问题点数:100、回复次数:6Top
1 楼careerist()回复于 2002-07-25 09:12:13 得分 0
先把unit2.pas加入到unit2.dll项目中,要在unit2.pas中写一段类似于ShowForm的接口函数.
再在unit1.pas中调用unit2.pas中的接口函数.Top
2 楼yball(yball)回复于 2002-07-25 09:18:51 得分 0
给个例子!谢谢!如果搞定的话,再加100分!Top
3 楼toplor(霜天晓竹)回复于 2002-07-25 09:26:15 得分 100
首先将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.
-------------------------------------------------------------------
风过西窗客渡舟船无觅处
年年一川新草遥看却似旧Top
4 楼gmc007(江西的佬表)回复于 2002-07-25 09:35:24 得分 0
如果你了解一点COM,我想这个问题很简单。
Top
5 楼careerist()回复于 2002-07-25 09:43:32 得分 0
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;
Top
6 楼yball(yball)回复于 2002-07-25 10:03:54 得分 0
ok,结帖加分!150+50Top
7 楼stiwin((忙碌))回复于 2002-07-25 10:12:37 得分 0
在Dll文件中uses你的窗体Top
8 楼xjl(...)回复于 2002-07-25 10:29:14 得分 0
标记一下Top




