小弟想做一个非模式的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窗体,请问哪位大侠可以帮忙????

解决方案 »

  1.   

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

  2.   

    还有就是 finally 改成 except
      

  3.   

    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;
      

  4.   

    测试通过:
    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.
      

  5.   

    睡到自然醒老兄:
    你的代码还是只能在按钮中显示DLL窗体的标题,没有办法整个DLL窗体显示出来!!!
      

  6.   

    按钮中显示DLL窗体的标题???是什么意思?
    我这里是整个FORM2都弹出来啦~!
      

  7.   

    把Form2的FormStyle设为fsStayOnTop看看是不是你要的效果?
      

  8.   

    不行啊,我试过啦,formstyle没用的
      

  9.   

    进入dll时不仅要传application,还要传screen
      

  10.   

    呵呵,我终于看明白了你问题出在哪里了!“你的代码还是只能在按钮中显示DLL窗体的标题,没有办法整个DLL窗体显示出来!!!”,你把句柄传错了!
        要传Application.Handle,就是Delphi创建的那个秘密窗体的Handle,你传的是Button的Handle, 这样的话,DLL中的窗体的父窗体就变成Button1了,所以呢,就只能显示一个标题了。
      

  11.   

    我是传Application.handle啊,我怎么不知道这一点呢?
      

  12.   

    unit Unit1;interfaceuses
      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.
      

  13.   

    try
          fp:=getprocaddress(h2,'sh_fm');
          sh_fm:=tsh_fm(fp);
          sh_fm(application.Handle,'DLL绐椾綋');   //显示窗体
          finally
            freelibrary(h2);                        //又把DLL资源施放掉
          end;试试象我那样静态的去调用Dll
      

  14.   

    静态调用,那什么时候释放资源啊?在一个项目中,DLL的目的就是节省资源??
      

  15.   

    你的Exe退出的时候就释放了,你也可以动态调用,不过用把DLL的Handle先保存起来
    当你的窗口释放的时候再FreeLibrary
      

  16.   

    那如果我的主程序要是调用过十几个DLL窗体,那不是要等主程序退出时一个个才会释放资源???
      

  17.   

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

  18.   

    主程序里放一个TList,TStringList,TObjectList都可以!
    打开Dll的时候就Add进去,这里的信息你可以自己设计,当然要有
    一个索引值,能让它找到正确的Handle;
    收到DLL发过来的释放信息的时候,信息里可以包含LIST的索引值
    主程序根据这个索引值找到正确的Handle后就示释放掉!
      

  19.   

    那我如何才能在DLL窗体中用一个"退出"按钮退出DLL窗体,返回到主程序啊?用DLLform.close或DLLform.free都会出错???
    顺便庆祝一下,刚才登录CSDN时的验证码为88888
      

  20.   

    要退出DLL窗体,直接在DLL窗体上进行退出就行了啊~
      

  21.   


    try
          fp:=getprocaddress(h2,'sh_fm');
          sh_fm:=tsh_fm(fp);
          sh_fm(application.Handle,'DLL绐椾綋');   //显示窗体
          finally
            freelibrary(h2);                        //又把DLL资源施放掉
          end;
    为什么我会报错的呢
      

  22.   

    sh_fm(application.Handle,'DLL绐椾綋');   //显示窗体
    报错
      

  23.   

    delphiBOX.COM上有这类源代码,你去找找呀,楼主
      

  24.   

    try
        form2.Caption:=cap;
        form2.Show;       //  form2.ShowModal 这样就不会闪一下不见了
        result:=true;
      finally
        application.Handle:=0;
        form2.Free;      //show 后马上就Free 当然就是闪一下就不见拉.
      end;
      

  25.   

    关于这个题目,我写过一篇论文,发表在《广西计算机科学》(好像是这个名字)上,你可以查查。我网站上也有部分内容:http://www.nnhy.org/HTML/23/48.htm
      

  26.   

    那我如何才能在DLL窗体中用一个"退出"按钮退出DLL窗体,返回到主程序啊?用DLLform.close或DLLform.free都会出错???
    你在OnClose的时候SendMessage(主程序的Handle,WM_EXIT自定义的消息,代表此DLL的索引号,0);
    主程序接受到后就对DLL的资源进行处理,我看你还是找篇完整点的文章看一下吧,估计这方面的文章不少.
    顺便庆祝一下,刚才登录CSDN时的验证码为88888   
    另外:88888  我都不只用遇到多少次了,呵呵!
      

  27.   

    按照你的程序步骤,显示的窗口是一闪而过.form2.Free这个语句应该在form2.Destory或Close事件中Free.还有一个Free的办法是在dll销毁的时候.
      

  28.   

    看看我的帖子
    http://blog.csdn.net/SmallMaker/archive/2007/06/05/1638908.aspx
      

  29.   

    楼主重写CreateParams在这里面有个设置父窗口的WndParent为你那个指定的窗口就可以实现了。
      

  30.   

    还有没有人会这个问题啊?用Tlist保存窗口的handle也不好解决啊?有没有真正的高手出现啊?