见题!提供范例和资料者均有分!!!!

解决方案 »

  1.   

    把你的program单元文件的代码改为如下:
    library 你将要生成的dll名称;//注意这里,改为了library,不是原来的program
    uses
      Forms,
      UnitFind in 'UnitFind.pas' {Form1};
    //上面是你用到的单元
    {$R *.res}
    exports //导出你调用dll中用到的函数或过程
       //函数或者过程名称 
       aaa;
    begin
    end.在你导出的函数或者过程中用代码来创建你的窗体.
    如:
    procedure aaa;
    try
     form1:=tform1.create(application);
     form1.showmodel;
    finally
     form1.free;
    end;
    然后在之前所见的export下面写上aaa;
    但要注意的是,如果你的过程需要返回值,且返回值为string型的,建议你改为pchar型返回
      

  2.   

    忘了你还要连接数据库,那么连接数据库的代码你就写在form1的创建事件中好了
      

  3.   

    使用DLL文件中封装的窗口一、在DLL中封装窗口
        打开Delphi新建一个DLL工程,保存为usedll,生成代码library usedll; { 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;{$R *.res}begin
    end.
    新建一个窗体,添加一个Label和Button,设置如下:object Form1: TForm1
    Left = 192
    Top = 133
    Width = 334
    Height = 221
    Caption = 'DLL'#20013#20351#29992#31383#20307
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    PixelsPerInch = 96
    TextHeight = 13
    object Label1: TLabel
    Left = 104
    Top = 80
    Width = 80
    Height = 13
    Caption = 'DLL'#20013#20351#29992#31383#20307
    end
    object Button1: TButton
    Left = 120
    Top = 152
    Width = 75
    Height = 25
    Caption = #30830#23450
    TabOrder = 0
    OnClick = Button1Click
    end
    end 添加一过程:procedure LoadForm; export;
    procedure LoadForm;
    begin
    Form1 := TForm1.Create(Application);
    try
    Form1.ShowModal;
    finally
    Form1.Free;
    end;
    end;全部完整的代码:library usedll;uses
    SysUtils,
    Classes,
    Form_Unit in 'Form_Unit.pas' {Form1};{$R *.res}
    exports
    LoadForm index 1;
    beginend.unit Form_Unit;interfaceuses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;type
    TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;var
    Form1: TForm1;
    ApplicationName: String;
    procedure LoadForm(Handle: THandle; AppName: ShortString); export;implementation{$R *.dfm}
    procedure LoadForm(Handle: THandle; AppName: ShortString);
    begin
    Application.Handle := Handle;
    ApplicationName := AppName;
    Form1 := TForm1.Create(Application);
    try
    Form1.ShowModal;
    finally
    Form1.Free;
    end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
    self.close;
    end;end.
    编译后生成usedll.dll文件,至此DLL文件就完成了二、调用DLL中封装的窗口     新建一个工程,添加一个Button,窗体布局如下:object Form1: TForm1
    Left = 192
    Top = 133
    Width = 336
    Height = 222
    Caption = 'Form1'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    PixelsPerInch = 96
    TextHeight = 13
    object Button1: TButton
    Left = 128
    Top = 88
    Width = 75
    Height = 25
    Caption = #25171#24320#31383#20307
    TabOrder = 0
    OnClick = Button1Click
    end
    end 完整的代码如下:unit Use_Unit;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;
    procedure LoadForm; external 'usedll.dll' index 1;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
    LoadForm;
    end;end.
    三、MDI-Child在DLL中载入并使用
        如果是MDI-Child又如何在DLL中载入并使用呢,下面就这个问题说说使用DLL文件中封装的窗口。
    新建一个DLL工程,保存为mdidll,再新建一个窗体,FormStyle设为fsMDIChild,如下:
    object Form1: TForm1
    Left = 192
    Top = 133
    Width = 344
    Height = 234
    Caption = 'MDI'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    FormStyle = fsMDIChild
    OldCreateOrder = False
    Position = poDefault
    Visible = True
    OnClose = FormClose
    PixelsPerInch = 96
    TextHeight = 13
    end代码如下:unit mdi_Unit;interfaceuses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs;type
    TForm1 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    private
    { Private declarations }
    public
    { Public declarations }
    MyParentForm: TForm;
    MyParentApplication: TApplication; 
    end;var
    DllApplication: TApplication;
    implementation{$R *.dfm}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
    Action:=caFree;
    end;end.library mdidll;uses
    Windows,
    Messages,
    SysUtils,
    Classes,
    Graphics,
    Controls,
    Forms,
    Dialogs,
    mdi_Unit in 'mdi_Unit.pas' {Form1};procedure LoadChild(ParentApplication: TApplication; ParentForm: TForm); export; stdcall;
    var
    Form1: TForm1;
    DllProc: Pointer; begin
    Application:=ParentApplication;
    Form1:=TForm1.Create(ParentForm);
    Form1.MyParentForm:=ParentForm;
    Form1.MyParentApplication:=ParentApplication;
    Form1.Show;
    end;procedure DLLUnloadProc(Reason: Integer); register;
    begin
    if Reason = DLL_PROCESS_DETACH then Application:=DllApplication;
    end;{$R *.res}
    exports
    LoadChild;
    begin
    DllApplication:=Application;
    DLLProc := @DLLUnloadProc;
    end.
    编译后生成mdidll.dll文件。
    使用DLL中的MDI-Child窗口如下:    新建一个工程,主窗口设置如下FormStyle设为fsMDIForm:
    object Form1: TForm1
    Left = 192
    Top = 133
    Width = 544
    Height = 375
    Caption = 'Form1'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    FormStyle = fsMDIForm
    Menu = MainMenu1
    OldCreateOrder = False
    PixelsPerInch = 96
    TextHeight = 13
    object MainMenu1: TMainMenu
    Left = 72
    Top = 136
    object N1: TMenuItem
    Caption = #26032#24314'(&N)'
    OnClick = N1Click
    end
    end
    end
    代码:unit usemdi_Unit;interfaceuses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, Menus;type
    TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    procedure N1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    T_ProvaChild = procedure (ParentApplication: TApplication; ParentForm: TForm); stdcall;
    var
    Form1: TForm1;implementation{$R *.dfm}procedure TForm1.N1Click(Sender: TObject);
    var
    DllHandle: THandle;
    ProcAddr: FarProc;
    ProvaChild: T_ProvaChild;
    begin 
    DllHandle := LoadLibrary('mdidll');
    ProcAddr := GetProcAddress(DllHandle, 'LoadChild');
    if ProcAddr <> nil then
    begin
    ProvaChild := ProcAddr;
    ProvaChild(Application,Self);
    end;
    end;end.
      

  4.   

    创建一个DLL模块。
    library  dll名称
    uses
      Forms,
      UnitFind in 'UnitFind.pas' {Form1};
    //上面是你用到的单元
    {$R *.res}
    exports //导出你调用dll中用到的函数或过程
           aaa;  //函数或者过程名称 
    begin
    end.在你导出的函数或者过程中用代码来创建你的窗体.
    如:
    procedure aaa;
    try
     form1:=tform1.create(application);
     form1.showmodel;
    finally
     form1.free;
    end;
      

  5.   

    多谢各位的帮助和关注!
    还有个问题。我的query的DATABASEName 设的是dbdemos之类的无须登陆的别名,那对数据库的操作没问题。但是如果我连的是MSSQL 这种类型的要登陆的,确总是提示“unknown user name or password ”。我用了tdatabase 且写了登陆事件了。我该怎么改,希望各位高手支援!!!
      

  6.   

    呵呵,这个简单了,给你例子给你
    //在dll中最好使用一个datamodel,然后在datamodel的create事件中写这些代码,最好你的dll窗体的创建也写在这些代码之后,然后在dll导出的函数中创建datamodel就可以了。
    try
       Session1.Active:=false;
       Database1.Connected:=false;
       Database1.Close;
       Database1.LoginPrompt:=false;
       Database1.Params.Clear;
       Database1.AliasName:=你的数据库别名;
       Database1.Params.Add('user name='+你的数据库用户名);
       Database1.Params.Add('password='+你的数据库密码);
       Database1.Connected:=true;
      except
       exit;
     end;
    但是要注意了,如果你的dll用的是ado组件来访问数据库,要写一段代码来控制dll接口的内存,不过你现在用的是bde,不会存在问题的
      

  7.   

    对,trustme007(相信我) 说得对,还要设置database的这个属性,我忘了,呵呵
      

  8.   

    照 lixiaohui(飞) 的方法,我加入了DataModule,用如下函数输出DataModule。
    function DATA_from():boolean;
    var
    DATA_from :TDataModule1;
    begin
      result:=false;
      DATA_from:= TDataModule1.Create(application);/////这句提示“undeclared    
      try                                             identifier:'application'”错误!
       result:=true;
       finally
       out_from.Free;
       end;end;我该怎么改?
    再次谢谢各位的援助!!!
      

  9.   

    呵呵,你把这个函数的实现写在哪儿了,算了,是我没有说清楚,贴个例子给你:
    dll的主单元:library kcFind;uses
      Forms,
      UnitFind in 'UnitFind.pas' {Form1};{$R *.res}
    exports
       findkc,findinputkc;
    beginend.dll中用到的窗体单元:unit UnitFind;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, StdCtrls, Buttons, Grids, DBGrids, ExtCtrls, DBTables,
      DB,inifiles;type
      TForm1 = class(TForm)//为了减少帖子长度,删除了不必要的东东
        procedure FormCreate(Sender: TObject);
        function getmodename:string;
      private
      public
      end;
    var
      Form1: TForm1;
      username,userno,modename,dbuser,dbpassword:string;
      procedure findkc;//定义要导出的dll函数implementation{$R *.dfm}procedure findkc;//dll导出的函数的实现部分
    begin
       try
         form1:=tform1.Create(application);
         form1.ShowModal;
       finally
         form1.Free;
       end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
         dbuser:=getusername;//我们这是将密码,数据库模式名,用户名都放在了ini文件中,下面我放了一个读取ini文件的例子
         dbpassword:=getpassword(getpasswordstring);//这里用了一个密码算法,当然,算法不能贴出来了
         modename:=getmodename;
         if (dbuser='')or(dbpassword='')or(modename='')then setini;//如果没有ini文件则自动写一个
         statusbar1.Panels[0].Text:='就绪';
     try
         Session1.Active:=false;
         Db1.Connected:=false;
         Db1.Close;
         Db1.LoginPrompt:=false;
         Db1.Params.Clear;
         Db1.AliasName:=modename;
         Db1.Params.Add('user name='+dbuser);
         Db1.Params.Add('password='+dbpassword);
         Db1.Connected:=true;
      except
         exit;
      end;
    end;function TForm1.getmodename: string;
    var
      inifile:tinifile;
    begin
      inifile:=nil;
    try
      inifile:=tinifile.Create(extractfiledir(application.ExeName)+'\logcfg.ini');
      result:=inifile.ReadString('DBSET','DataBase','');
    finally
      inifile.Free;
    end;
    end;end.
      

  10.   

    在上面的例子中没有用到datamodel,但这个是不影响的。同理照推则可。
      

  11.   

    窗体重用最好是作成一个frame来使用。