如何delphi动态调用封装在dll里的窗体
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. 显示结果:
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. 显示结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询