静态/动态调用dll封装窗体(模态/非模态)

页面导航:首页 > 软件编程 > Delphi > 静态/动态调用dll封装窗体(模态/非模态)

静态/动态调用dll封装窗体(模态/非模态)

来源: 作者: 时间:2016-01-18 15:42 【

delph XE下测试通过.Dll项目文件[delphi] library Project10; uses SysUtils, Classes, Forms, Unit14 in #39;Unit14.pas#39; {Form1}; {$R *.res} Function EDebtMoney(H: THandle):...
 
delph XE下测试通过.
 
Dll项目文件
[]  
library Project10;  
  
uses  
  SysUtils,  
  Classes,  
  Forms,  
  Unit14 in 'Unit14.pas' {Form1};  
  
{$R *.res}  
  
Function EDebtMoney(H: THandle): integer;  
begin  
  Application.Handle := H;  
  with TForm1.Create(Application) do  
    try  
      ShowModal;  
      Result := 0;  
    finally  
      Free; { 调用结束时销毁窗口 }  
    end;  
end;  
  
Function GetForm(H: THandle): THandle;  
begin  
  Application.Handle := H;  
  with TForm1.Create(Application) do  
  try  
    Show;  
    Result := Handle;  
  except on e : Exception do  
    raise e.Create(e.Message);  
  end;  
end;  
  
exports EDebtMoney, GetForm;  
begin  
  
end.  
 
 
dll中窗体关闭事件
[delphi]  
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);  
begin  
  Action := caFree;  
end;  
 
 
调用单元
[delphi]  
unit Unit13;  
  
interface  
  
uses  
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  Dialogs, StdCtrls;  
  
type  
  TForm13 = class(TForm)  
    btn1: TButton;  
    btn2: TButton;  
    procedure btn1Click(Sender: TObject);  
    procedure btn2Click(Sender: TObject);  
  private  
    { Private declarations }  
  public  
    { Public declarations }  
  end;  
  
var  
  Form13: TForm13;  
  function MyForm(H: THandle) : THandle; stdcall;  
implementation  
  
function MyForm; external 'Project10.dll' name 'GetForm';  
{$R *.dfm}  
  
//动态调用  模态窗口  
procedure TForm13.btn1Click(Sender: TObject);  
type  
  TGetForm = function (H: THandle) : Integer; cdecl;  
var  
  DllForm : TGetForm;  
  DllHandle : THandle;  
  nn : integer;  
begin  
  DllHandle := LoadLibrary(PChar('Project10.dll'));  
  
  try  
    if DllHandle <> 0 then  
    begin  
      DllForm := GetProcAddress(DllHandle, 'EDebtMoney');  
      nn := DllForm(Application.Handle) ;  
      self.Caption  := inttostr(nn);  
    end;  
  finally  
    FreeLibrary(DllHandle);  
  end;  
  
end;  
//静态调用  非模态窗口  
procedure TForm13.btn2Click(Sender: TObject);  
var  
  DllHandle : THandle;  
begin  
  DllHandle := MyForm(Application.Handle);  
end;  
  
end.  
 
Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<