这个DEMO的是一个定义了一个窗体插件接口,把其他窗口注册到这个窗体插件接口中。主程序运行起来,就遍历一下窗体插件接口,把每个窗体内嵌到对话框中

运行效果如下

主窗口代码如下

unit Frm_Main;

interface

{
本程序演示了如何使用QPlugins来做模块间的松散耦合,使用了Execute来传递,实际上
也可以约定接口直接调用(请参考 MultiInstance 示例)
}
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
QPlugins,
qplugins_base,
qplugins_params,
Vcl.StdCtrls,
Vcl.ComCtrls; type
TForm_Main = class(TForm)
PageControl1: TPageControl;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form_Main: TForm_Main; implementation {$R *.dfm} // 创建主窗口
procedure TForm_Main.FormCreate(Sender: TObject);
var
ARoot: IQServices;
I: Integer;
ATabSheet: TTabSheet;
AParams: IQParams;
begin
// 通过路径获取指定的服务接口实例
ARoot := PluginsManager.ByPath('Services/Docks') as IQServices;
// 如果存在
if Assigned(ARoot) then
begin
// 创建参数
AParams := TQParams.Create;
AParams.Add('Parent', ptUInt64);
// 遍历实例
for I := to ARoot.Count - do
begin
// 每个实例都创建一个内嵌窗体
ATabSheet := TTabSheet.Create(PageControl1);
ATabSheet.PageControl := PageControl1;
ATabSheet.Caption := ARoot[I].Name;
//
AParams[].AsInt64 := IntPtr(ATabSheet);
ARoot[I].Execute(AParams, nil);
end;
end;
end; end.

服务单元如下

unit Serv_Dock;

interface

uses
classes,
qstring,
QPlugins,
qplugins_params,
qplugins_base,
controls; type
TDockService = class(TQService)
private
FControlClass: TControlClass;
public
function Execute(AParams: IQParams; AResult: IQParams): Boolean; override; stdcall;
property ControlClass: TControlClass read FControlClass write FControlClass;
end; const
// 窗口插件接口
IDockServices: TGUID = '{9DDD6DD9-3053-4EE2-90D5-759267DBB10C}'; // 注册插件窗体
procedure RegisterDock(AClass: TControlClass); implementation { TDockService } // 执行服务并将结果返回到AResult中
function TDockService.Execute(AParams, AResult: IQParams): Boolean;
var
AParent: TWinControl;
AControl: TControl;
begin
// 第一个参数为父窗口句柄
AParent := Pointer(AParams[].AsInt64);
// 窗体内嵌,并返回真
AControl := ControlClass.Create(AParent);
AControl.HostDockSite := AParent;
AControl.Visible := True;
AControl.Align := alClient;
Result := True;
end; // 注册插件窗体
procedure RegisterDock(AClass: TControlClass);
var
AParent: IQServices;
AService: TDockService;
begin
// ById通过服务接口ID获取服务接口实例,唯一的IDockServices
AParent := PluginsManager.ById(IDockServices) as IQServices;
// 创建TDockService服务
AService := TDockService.Create(NewId, AClass.ClassName);
AService.ControlClass := AClass;
// 把服务添加到服务接口实例中
AParent.Add(AService);
end; procedure RegisterClass;
begin
// 注册内嵌窗体接口
PluginsManager.Services.Add(TQServices.Create(IDockServices, 'Docks'));
end; initialization // 注册
RegisterClass; end.

窗体1

unit Frm_Show1;

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
TForm_Show1 = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
Label1: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end; var
Form_Show1: TForm_Show1; implementation uses
Serv_Dock;
{$R *.dfm} initialization // ById通过服务接口ID获取IDockServices服务接口实例,并把这个TForm_Show1注册到接口实例中
RegisterDock(TForm_Show1); end.

窗体2

unit Frm_Show2;

interface

uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Variants,
System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls,
Vcl.ExtCtrls,
Vcl.Imaging.jpeg; type
TForm_Show2 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Image1: TImage;
Label2: TLabel;
Label3: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end; var
Form_Show2: TForm_Show2; implementation uses
Serv_Dock;
{$R *.dfm} initialization // ById通过服务接口ID获取IDockServices服务接口实例,并把这个TForm_Show2注册到接口实例中
RegisterDock(TForm_Show2); end.

013.Delphi插件之QPlugins,模块化代码示例的更多相关文章

  1. 基于RabbltMQ延迟插件实现延迟队列代码示例

    上一篇文章写了docker安装RabbitMQ及延迟插件的安装,这篇的话是基于RabbitMQ延迟插件实现延迟队列的示例 那么废话不多说 直接上代码!! 首先创建延迟队列配置类 DelayedQueu ...

  2. 015.Delphi插件之QPlugins,FMX插件窗口

    内嵌FMX的插件窗口,效果还是很可以的.退出时,会报错,很诡异啊. 主窗口代码如下 unit Frm_Main; interface uses Winapi.Windows, Winapi.Messa ...

  3. 014.Delphi插件之QPlugins,MDI窗口

    不知道为什么,这个DEMO编译出来报错,运行不了,在QDAC群里问了一下也没人响应. 效果如下 主程序代码如下 unit Frm_Main; interface uses Winapi.Windows ...

  4. 012.Delphi插件之QPlugins,多实例内嵌窗口服务

    这个DEMO中主要是在DLL中建立了一个IDockableControl类,并在DLL的子类中写了具体的实现方法. 在主程序exe中,找到这个服务,然后调用DLL的内嵌方法,把DLL插件窗口内嵌到主程 ...

  5. 011.Delphi插件之QPlugins,延时加载服务

    这个DEMO是是把DLL插件的相关信息做成了一个配置文件,主程序加载这个配置文件,从而起到延时加载的作用 主程序代码如下 unit Frm_Main; interface uses Winapi.Wi ...

  6. 010.Delphi插件之QPlugins,遍历服务接口

    这个DEMO注意是用来看一个DLL所拥有的全部服务接口 演示效果如下 代码如下: unit Frm_Main; interface uses Winapi.Windows, Winapi.Messag ...

  7. 009.Delphi插件之QPlugins,服务的热插拔

    这个DEMO用来演示服务的替换,用起来总是怪怪的感觉,效果图如下 代码如下 unit Frm_Main; interface uses Winapi.Windows, Winapi.Messages, ...

  8. 007.Delphi插件之QPlugins,插件的卸载和重新加载

    效果图如下,可以反复卸载和重新加载.QPlugins这个插件,还没弄明白,摸索着跟着DEMO写 主窗口代码如下 unit Frm_Main; interface uses Winapi.Windows ...

  9. 005.Delphi插件之QPlugins,IQNotify通知

    演示的界面如下,拖动滚动条,百分比圆和进度条也是会跟着动的 主程序的代码如下 unit Frm_Main; interface uses Winapi.Windows, Winapi.Messages ...

随机推荐

  1. B: 最小代价

    B: 最小代价 题解:先用最小生成树求联通所有点的最小代价ans 在求度为1的时候权值最大的点mx ans-mx就是答案 #include<iostream> #include<al ...

  2. Python流程控制-3 循环控制

    循环控制,就是让程序循环运行某一段代码直到满足退出的条件,才退出循环. Python用关键字for和while来进行循环控制,但是没有其它语言的do...while语句(在Java和PHP中都有do ...

  3. eclipse导入项目上面有个红叉X

    问题: 今天突然想到一个以前做过的项目,想导入到新环境中,发现不管咱整都一个红叉X, 我记得以前好像碰到过类似的问题,当时三秒搞定,谁知道时间一长,三分钟没有搞定. 还是记录下: 一般导入项目出错,肯 ...

  4. 分布式事务中间件 TCC-Transaction 源码分析 —— 项目实战

    https://blog.csdn.net/lldouble/article/details/79455172

  5. Day11-G - Calendar Game HDU - 1079

    Adam and Eve enter this year’s ACM International Collegiate Programming Contest. Last night, they pl ...

  6. prototype入门----自定义创建元素

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. ubuntu 中怎么安装 jdk 7

    Jdk1.7 安装包的下载地址是: http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u4-downloads-1591156. ...

  8. Lamda表达式学习笔记一

    Lamda表达式学习笔记一 一.Lamda语法诠释 三傻大闹宝莱坞的主人公兰彻说的一句话让我映像深刻:用简单的语言来表达同样的意 我并不是说书上的定义怎么怎么不对,而是应该理解书本上的定义,并用简单的 ...

  9. CentOS安装后的第一步:配置IP地址

    有关于centos7获取IP地址的方法主要有两种,1:动态获取ip:2:设置静态IP地址 在配置网络之前我们先要知道centos的网卡名称是什么,centos7不再使用ifconfig命令,可通过命令 ...

  10. _CrtIsValidHeapPointer(pUserData)

    程序遇到如题的运行时报错,参考下面这段文字,采取将自定义类的对象定义改为new方式生成后问题解决. !!Expression: _CrtIsValidHeapPointer(pUserData) vo ...