Delphi 调用dll中的窗体

1.定义窗体

unit UDllForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TDllForm = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DllForm: TDllForm;
  procedure ShowDllFormInPanel(Parent:THandle);stdcall;
  procedure ShowDllForm;stdcall;
implementation

procedure ShowDllFormInPanel(Parent:THandle);stdcall;
begin
  Application.handle:=parent;
  if DllForm = nil then
    DllForm:= TDllForm.Create(Application);
  DllForm.ParentWindow:=Parent;//将容器设置为父窗口
  DllForm.Show;
end;

procedure ShowDllForm;stdcall;
begin
  if DllForm = nil then
    DllForm:= TDllForm.Create(Application);
  DllForm.Show;
end;

{$R *.dfm}

procedure TDllForm.btn1Click(Sender: TObject);
begin
  ShowMessage('HELLO');
end;

end.

2.定义dll

library Formindll;

uses
  SysUtils,
  Classes, 
  UDllForm in 'UDllForm.pas' {DllForm};

{$R *.res}
exports
   ShowDllFormInPanel,
   ShowDllForm;
begin
end.
3.dll窗体调用 

     注意:dll窗体不能直接放在父窗体里面 ,这里可以使用panel作为父窗体 对应过程ShowDllFormInPanel

                 dll窗体不放在父窗体 则直接使用ShowDllForm 过程,想一个对话框的形式

unit Unit1;

interface

uses
  SysUtils,Classes,Forms,Windows,Messages, Controls, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    pnl1: TPanel;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  procedure ShowDllFormInPanel(Parent:THandle);stdcall; external 'E:\test7\formindll.dll';
  procedure ShowDllForm;                       stdcall; external 'E:\test7\formindll.dll';
implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
    ShowDllFormInPanel(pnl1.Handle);
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
    ShowDllForm;
end;

end.




你可能感兴趣的:(delphi)