小弟想做一个非模式的DLL窗体,form2函数用来输出,
function form2(h:thandle;cap:string):boolean;stdcall;
var
Form2: TForm2;
begin
result:=false;
application.Handle:=h;
form2:=tform2.Create(application);
try
form2.Caption:=cap;
form2.Show;
result:=true;
finally
application.Handle:=0;
form2.Free;
end;
end;
但是在主窗体调用form2函数时,这个窗体总是闪一下就不见了,
我想让它显示在调用窗体之上,但又不是模式窗体,这样我可以操作调用窗体.
例如:A调用显示出来B窗体,B窗体在A之上,但又必须可以在不关闭B的同时可以操作A窗体,请问哪位大侠可以帮忙????
function form2(h:thandle;cap:string):boolean;stdcall;
var
Form2: TForm2;
begin
result:=false;
application.Handle:=h;
form2:=tform2.Create(application);
try
form2.Caption:=cap;
form2.Show;
result:=true;
finally
application.Handle:=0;
form2.Free;
end;
end;
但是在主窗体调用form2函数时,这个窗体总是闪一下就不见了,
我想让它显示在调用窗体之上,但又不是模式窗体,这样我可以操作调用窗体.
例如:A调用显示出来B窗体,B窗体在A之上,但又必须可以在不关闭B的同时可以操作A窗体,请问哪位大侠可以帮忙????
如果不行,你就把application传进来,替换掉dll的application,而不是application.handle
//var //去掉声明,直接用Unit2的Form2全局变量
// Form2: TForm2;
begin
result:=false;
application.Handle:=h;
if form2 = nil then //增加判断
form2:=tform2.Create(application);
try
form2.Caption:=cap;
form2.Show;
result:=true;
finally
application.Handle:=0;
// form2.Free; //这里不要
end;
end;
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,
Windows,
Classes,
Forms,
Unit2 in 'Unit2.pas' {Form2};{$R *.RES}function showform(h:THandle; cap:string):boolean;stdcall;
begin
result:=false;
application.Handle:=h;
if Form2 = nil then
Form2:=tform2.Create(application);
try
form2.Caption:=cap;
form2.Show;
result:=true;
finally
application.Handle:=0;
// form2.Free;
end;
end;exports
showform;begin
end.
EXE:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ 私有成员(变量、函数)声明 }
public
{ 公共成员(变量、函数)声明 }
end; function showform(h:thandle;cap:string):boolean;stdcall;external 'Project2.dll';var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
showform(Application.handle,'abc');
end;end.
你的代码还是只能在按钮中显示DLL窗体的标题,没有办法整个DLL窗体显示出来!!!
我这里是整个FORM2都弹出来啦~!
要传Application.Handle,就是Delphi创建的那个秘密窗体的Handle,你传的是Button的Handle, 这样的话,DLL中的窗体的父窗体就变成Button1了,所以呢,就只能显示一个标题了。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
type tsh_fm=function(h:thandle;cap:string):boolean;stdcall;
var
sh_fm:tsh_fm;
fp:tfarproc;
h2:thandle;
begin
h2:=loadlibrary('DllPro.dll');
if h2>0 then
begin
try
fp:=getprocaddress(h2,'sh_fm');
sh_fm:=tsh_fm(fp);
sh_fm(application.Handle,'DLL绐椾綋');
finally
freelibrary(h2);
end;
end;end;end.unit dllunit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ComboBox1: TComboBox;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function sh_fm(h:thandle;cap:string):boolean;stdcall;
implementation{$R *.dfm}
function sh_fm(h:thandle;cap:string):boolean;stdcall;
begin
result:=false;
application.Handle:=h;
if form1=nil then
form1:=tform1.Create(application);
try
form1.Caption:=cap;
form1.Show;
result:=true;
finally
application.Handle:=0;
end;
end;
end.
fp:=getprocaddress(h2,'sh_fm');
sh_fm:=tsh_fm(fp);
sh_fm(application.Handle,'DLL绐椾綋'); //显示窗体
finally
freelibrary(h2); //又把DLL资源施放掉
end;试试象我那样静态的去调用Dll
当你的窗口释放的时候再FreeLibrary
在DLL里面,如果窗体关闭,也就是不再需要资源了,这个时候发个消息
给主程序,主程序在收到消息后,在列表里面查找适合的DLL的HANDLE进行释放!
打开Dll的时候就Add进去,这里的信息你可以自己设计,当然要有
一个索引值,能让它找到正确的Handle;
收到DLL发过来的释放信息的时候,信息里可以包含LIST的索引值
主程序根据这个索引值找到正确的Handle后就示释放掉!
顺便庆祝一下,刚才登录CSDN时的验证码为88888
try
fp:=getprocaddress(h2,'sh_fm');
sh_fm:=tsh_fm(fp);
sh_fm(application.Handle,'DLL绐椾綋'); //显示窗体
finally
freelibrary(h2); //又把DLL资源施放掉
end;
为什么我会报错的呢
报错
form2.Caption:=cap;
form2.Show; // form2.ShowModal 这样就不会闪一下不见了
result:=true;
finally
application.Handle:=0;
form2.Free; //show 后马上就Free 当然就是闪一下就不见拉.
end;
你在OnClose的时候SendMessage(主程序的Handle,WM_EXIT自定义的消息,代表此DLL的索引号,0);
主程序接受到后就对DLL的资源进行处理,我看你还是找篇完整点的文章看一下吧,估计这方面的文章不少.
顺便庆祝一下,刚才登录CSDN时的验证码为88888
另外:88888 我都不只用遇到多少次了,呵呵!
http://blog.csdn.net/SmallMaker/archive/2007/06/05/1638908.aspx