delphi调用带有窗体的dll,窗体的事件怎样调用?

2025-02-07 17:51:04
推荐回答(2个)
回答(1):

以下是我编写的示例代码,完成这个示例,需要三个文件:

1. dll工程文件, test.dll:

library testDll;

uses

  SysUtils,

  Classes,

  Forms,

  FormDll in 'FormDll.pas' {frmDll};

{$R *.RES}

function GetDllForm: TForm; stdcall; export;

begin

  Result := frmDll;

end;

exports

  GetDllForm;

begin

end.

2. dll中包含的窗体,窗体有一个按钮,FormDll.pas:

unit FormDll;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;

type

  TfrmDll = class(TForm)

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  frmDll: TfrmDll;

implementation

{$R *.dfm}

procedure TfrmDll.Button1Click(Sender: TObject);

begin

  MessageDlg('你已经成功使用了DLL窗体。',  mtInformation, [mbOK], 0);

end;

initialization

begin

  frmDll := TfrmDll.Create(Application);

end;

finalization

begin

  frmDll.Free;

end;

end.

3. 调用dll窗体的文件,testdll.pas:

unit testdll;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;

type

  TForm2 = class(TForm)

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form2: TForm2;

function GetDllForm: TForm;

implementation

{$R *.dfm}

function GetDllForm: TForm; external 'testdll.dll';

procedure TForm2.Button1Click(Sender: TObject);

var

  AForm: TForm;

begin

  AForm := GetDLLForm;

  AForm.Show;

end;

end.

4. 显示结果:

回答(2):

这得看你怎么声明dll中导出的那个函数的
给你个例程
dll中声明导出的函数
procedure ShowDllForm;stdcall;
begin
if Form1 = nil then
begin
Form1 := TForm1.Create(Application);
Form1.Show;
end else if not Form1.Showing then
Form1.Show;
end;