读书人

绝对高手的挑战?关于DLL窗体的调用 ?该

发布时间: 2012-02-14 19:19:19 作者: rapoo

绝对高手的挑战????关于DLL窗体的调用 ???
小弟想做一个非模式的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窗体,请问哪位大侠可以帮忙????


[解决办法]
你把application.Handle:=0;去掉试试看.
如果不行,你就把application传进来,替换掉dll的application,而不是application.handle

[解决办法]
function form2(h:thandle;cap:string):boolean;stdcall;//form2改成其他名字,例如ShowForm
//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;

interface

uses
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.
------解决方案--------------------


try
fp:=getprocaddress(h2, 'sh_fm ');
sh_fm:=tsh_fm(fp);
sh_fm(application.Handle, 'DLL绐 '); //显示窗体
finally
freelibrary(h2); //又把DLL资源施放掉
end;

试试象我那样静态的去调用Dll
[解决办法]
所以一般的做法是,用一个列表保存你已经打开的Dll的Handle;
在DLL里面,如果窗体关闭,也就是不再需要资源了,这个时候发个消息
给主程序,主程序在收到消息后,在列表里面查找适合的DLL的HANDLE进行释放!
[解决办法]

try
fp:=getprocaddress(h2, 'sh_fm ');
sh_fm:=tsh_fm(fp);
sh_fm(application.Handle, 'DLL绐 '); //显示窗体
finally
freelibrary(h2); //又把DLL资源施放掉
end;
为什么我会报错的呢
[解决办法]
delphiBOX.COM上有这类源代码,你去找找呀,楼主
[解决办法]
try
form2.Caption:=cap;
form2.Show; // form2.ShowModal 这样就不会闪一下不见了
result:=true;
finally
application.Handle:=0;
form2.Free; //show 后马上就Free 当然就是闪一下就不见拉.
end;
[解决办法]
那我如何才能在DLL窗体中用一个 "退出 "按钮退出DLL窗体,返回到主程序啊?用DLLform.close或DLLform.free都会出错???
你在OnClose的时候SendMessage(主程序的Handle,WM_EXIT自定义的消息,代表此DLL的索引号,0);
主程序接受到后就对DLL的资源进行处理,我看你还是找篇完整点的文章看一下吧,估计这方面的文章不少.
顺便庆祝一下,刚才登录CSDN时的验证码为88888
另外:88888 我都不只用遇到多少次了,呵呵!
[解决办法]
按照你的程序步骤,显示的窗口是一闪而过.

form2.Free这个语句应该在form2.Destory或Close事件中Free.

还有一个Free的办法是在dll销毁的时候.
[解决办法]
楼主重写CreateParams在这里面有个设置父窗口的WndParent为你那个指定的窗口就可以实现了。

读书人网 >.NET

热点推荐