程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 一個值得大家來考慮的DLL問題

一個值得大家來考慮的DLL問題

編輯:Delphi

  在MDI窗體類型的編程中,主窗體(fsMDIForm)怎樣調用子窗體(fsMDIChild,此子窗體為DLL)。
  以下是我的原代碼:
  ==========================================================================================
  DPR  單元代碼

  program Mdiform;

  uses
    Forms,
    UMdiform in 'UMdiform.pas' {Mainform},
    UDM in 'UDM.pas' {GlobalDM: TDataModule},
    UFun in 'UFun.pas';

  {$R *.res}

  begin
    Application.Initialize;
    Application.CreateForm(TMainform, Mainform);
    Application.Run;
  end.
  ===============================================================================================

  主窗體代碼:
  unit UMdiform;
  interface
  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, Udm,StdCtrls;
  type
   
    TMainform = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
      procedure FormCreate(Sender: TObject);
    private
      { Private declarations }
    public
       MyHandle:HWND;
      { Public declarations }
    end;

  var
    Mainform: TMainform;

  type
    T_ShowTestMng=function (var adm:TMainform) : Boolean; StdCall;

  implementation
  {$R *.dfm}

  procedure TMainform.Button1Click(Sender: TObject);
  var
    Lib_         :THandle;
    _ShowTestMng :T_ShowTestMng;
  begin
    Lib_:=LoadLibrary(pchar('MdiChild.dll'));
    try
      @_ShowTestMng:=GetProcAddress(Lib_,'_ShowTestMng');
      if not(@_ShowTestMng=nil) then
        _ShowTestMng(Mainform);
    finally
      FreeLibrary(Lib_);
    end;
  end;

  procedure TMainform.FormCreate(Sender: TObject);
  begin
   MyHandle:=Application.Handle;
  end;

  end.
  ==============================================================================================

  子窗體DLL代碼:
  library MdiChild;

  uses
    ShareMem,
    UMdiform,  //此單元為父窗體單元,在頂目設置中我已經設置了搜索此單元在路徑。
    Forms,
    SysUtils,
    Classes,
    UChild in 'UChild.pas' {FrmChild};//FrmChild子窗體的FormStyle屬性為FsMDIChild

  {$R *.res}
  function _ShowTestMng(var adm:TMainform) : Boolean; StdCall;
  begin
    result:=true;
    Application.Handle:=adm.MyHandle;
    Application.CreateForm(TFrmChild,FrmChild);   //程序就出錯在此:出錯原因是:Cannot create form. No MDI Forms are currently active.
    FrmChild.Show;
  end;

  exports
      _ShowTestMng;
  end.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved