本人只对WINDOWS编程有基本的了解。现在想将窗体编成动态库,我想做个窗体,一个文本框。我在其它程序中调用这个动态库时,这个窗体显示出来且文本框内的内容为调用此窗体的窗体的标题(caption),我可以通过这个文本框修改这个caption,请问有谁会这样做法!

解决方案 »

  1.   

    本人只对WINDOWS编程有基本的了解。现在想将窗体编成动态库,我想做个窗体,一个文本框。我在其它程序中调用这个动态库时,这个窗体显示出来且文本框内的内容为调用此窗体的窗体的标题(caption),我可以通过这个文本框修改这个caption,请问有谁会这样做法!可以这样来做:
      在导出函数里把调用窗体的句柄传入DLL,然后DLL可以通过该句柄取得CAPTION,也可改变CAPTION的值。
      当然,应该在DLL窗体单元声明一个变量来保存句柄,为了将句柄传入,应该重载构造函数。
    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,
      Classes,
      forms,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}
    function LoadForm(AHandle:THandle;FHandle:longint):longint;stdcall;
    var
      DLLForm:TForm;
    begin
      APPLication.Handle:=AHandle;
      DLLForm:=TForm1Class(FindClass('TForm1')).Create(nil,FHandle);
      try
        DLLForm.Show;
        Result:=longint(DLLForm);
      except
        DLLForm.Free;
      end;
    end;exports
      LoadForm;
    beginregisterclass(TForm1);
    end.unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1Class = Class of TForm1;
      TForm1 = class(TForm)
        Edit1: TEdit;
        Edit2: TEdit;
        procedure Edit1Exit(Sender: TObject);
      private
        { Private declarations }
      public
        FMain:longint;
        constructor Create(AOwner: TComponent); overload;
        constructor Create(AOwner: TComponent;AHandle:longint); overload;
        { Public declarations }
      end;
    implementation{$R *.dfm}constructor TForm1.Create(AOwner: TComponent);
    beginend;constructor TForm1.Create(AOwner: TComponent; AHandle: Integer);
    begin
       FMain:=AHandle;
       inherited create(AOwner);
       edit1.Text:=TForm(FMain).Caption;
    end;procedure TForm1.Edit1Exit(Sender: TObject);
    begin
      TForm(FMain).Caption:=edit1.Text;
    end;end调用程序代码:
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    function LoadForm(AHandle:THandle;FHandle:longint):longint;stdcall external 'project2.dll' name 'LoadForm';procedure TForm1.Button1Click(Sender: TObject);
    var
      dd:longint;
    begin
      dd:=LoadForm(APPLication.Handle,longint(form1));end;end.