Delphi 封装Frame到Dll文件


本文整理自网络,侵删。

 
做项目的时候,发现这个Frame很好用,为了省空间、调用和修改方便,就将Frame封装到dll(动态链接库)里面,确实很好使。

效果图如下:

上图是临时测试用的,忘了将Frame的align设置成alClient,不过刚好可以看出来白色区域是从dll里加载的Frame,还调用了Frame的按钮单击事件。

下面是大概的建立流程:

1、File -> New -> Other -> Delphi Projects -> 新建一个Dll文件



2、然后继续 File -> New -> Other -> Delphi Projects -> Delphi Files -> VCL Frame,新建一个Frame窗体

在dll里新建非Form的窗口视窗程序时,会弹出如下对话框,英文不去管它,直接点Yes,No的话我没试过,不确定啥后果。。

3、在Frame上随便放一些什么组件,这里我放置了一个Memo和Button来测试效果,如下图:

4、然后写上Button组件的OnClick事件,用于测试。

并在Frame单元的程序代码实现部分,{$R *.dfm}这个编译指令下面,写上一个创建Frame的过程,其他dll的相关操作和规范要求可以百度。

此处需要注意,需要手动定义一个 Frame(名字可以任意取): TFrame1(类名就是Frame的Name值,也可以任意取)。

5、编译并创建dll文件,我习惯右键Build。

快捷键我不熟悉,另附万一老师的快捷键大全:http://www.cnblogs.com/del/archive/2007/12/04/982851.html
编译后就得到了一个封装了Frame的Dll文件可供调用了,如下图:

6、最后新建一个Exe应用,静态或者动态调用Dll文件。

完整代码如下:


//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

相关阅读 >>

Delphi tbutton.onclick 匿名函数用法

Delphi 根据盘符弹出u盘

Delphi通过调用com对象实现更改桌面壁纸

Delphi 在rxrichedit中插入图片的完美解决方法

Delphi stringgrid 加载excel表格文件内容自动宽度

两种方法用Delphi实现域名转ip地址---用nmhttp控件和winsock

Delphi application.restore不起作用了,该如何处理

Delphi webbrowser获取iframe页面内容

Delphi正则表达式

Delphi 请求时间,为当前时间,数值为1970-01-01以来的毫秒数

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...