怎么用DLL实现窗体的重用????具体一点最好.

解决方案 »

  1.   

    你将窗体封装在dll中调用不就好了
      

  2.   

    新建DLL程序,添加窗体,在程序中动态或静态引用就行了.
    动态调用: Windows API loadlibrary getprocaddress(好象是) freelibrary,调用DLL中的showform方法,当然你要先编好.
      

  3.   

    告诉你一个涉及程序整体框架式,考虑使用的一个方法;
    Dll,interface,MainApplication;1、先定义一个接口:
    *****************************
    unit myinterface;interfacetype
      ImyTest = interface
      ['{8068AF74-187F-4847-9A5D-915CB4AF8FC7}']
        function F1:integer;
      end;
      myTestClass = Class(TinterfacedObject,ImyTest)
        function F1:integer;virtual;abstract;
      end;implementation{ myTestClass }{
    function myTestClass.F1: integer;
    begin
      result := 999;
    end;
    }end.
    *****************************
    2、在动态库中对其接口进行实现;
    ****************************
    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,
      myinterface,
      Classes;type
      myTest = Class(myTestClass)
        function F1:integer;override;
      end;  {$R *.res}{ myTest }function getMyClass:myTest;
    var
      my:myTest;
    begin
        my := myTest.Create();
        result := my;
    end;
    exports
      getMyClass;
    { myTest }function myTest.F1: integer;
    begin
      result := 1000;
    end;begin
    end.
    当然这就是你封在动态库里的窗体,只是将父类改称冲接口继承的类即可;
    ********************************
    3、主程序引用接口,调用动态库中的窗体,调用其中的功能函数;
    ********************************
    unit main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,myinterface;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      function getMyClass:myTestClass;external 'dll\Project2.dll';
    var
      Form1: TForm1;implementation
    {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      my:ImyTest;
    begin
      my := getMyClass().Create();
      showmessage(inttostr(my.F1));
    end;end.
    ***********************************
    这样实现了功能模块与主程序模块的或者其他模块的分离,降低了耦合性;
    ***********************************
    问题的引出
    ***********************************
    通常有些人,想把窗体封装到动态库中,这就涉及到一个问题;
    1、如果动态库中的窗体有自己的功能函数,主程序如果想调用,是看不见的,
    只有引用动态库中的窗体,这样主程序就和动态库中的窗体的耦合性增加了;
    另一方面,动态库的编写者的代码被公开了;容易被修改,导致整体项目的混乱;
    2、所以应用接口的方法,实现了两者的分离;程序的整体结构以维护,小组同时工作
    的可能性增加;个人观点,仅供参考