2、然后继续 File -> New -> Other -> Delphi Projects -> Delphi Files -> VCL Frame,新建一个Frame窗体
完整代码如下:
//Dll文件本身
library Project1;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
System.SysUtils,
System.Classes,
Unit1 in 'Unit1.pas' {Frame1: TFrame};
{$R *.res}
begin
end.
//Dll封装的Frame单元
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TFrame1 = class(TFrame)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Frame1: TFrame1; //此处手动添加
implementation
{$R *.dfm}
procedure TFrame1.Button1Click(Sender: TObject);
begin
Memo1.Clear;
Memo1.Lines.Add('Hello Delphi! Hello World!' + #13#10 + 'Do You Love This World?');
end;
//Frame创建过程
procedure getFrameRun(Parent:THandle); stdcall; export;
begin
Application.handle := Parent;
//将容器设为应用程序句柄
//以非模态创建并显示窗口
if Frame1 = nil then
Frame1 := TFrame1.Create(Application);
Frame1.ParentWindow := Parent; //将容器设置为父窗口
Frame1.Show;
end;
//exports
exports
getFrameRun;
end.
//调用Dll的程序单元
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
//dll文件如果不在同一文件夹就加上路径
procedure getFrameRun(Parent:THandle); stdcall; external 'Project1.dll';
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
getFrameRun(Panel1.Handle); //静态调用创建Frame1
end;
end.
来源:https://www.cnblogs.com/go-jzg/p/4114136.html