Delphi封装Mdi窗体到Dll并使用插件管理,tabControl制作多页面

本文主要是介绍Delphi封装Mdi窗体到Dll并使用插件管理,tabControl制作多页面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

源码下载地址


1.ShareMem的引用要放在各单元的第一位置,否则会报错

2.dll中mdi子窗体关闭时要,

     Action:=caFree;
    TestForm2:=nil;

3.




主窗体代码

unit MainUnit;interfaceusesShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, ComCtrls, Menus, ToolWin, RzTabs,StrUtils;typeTTestdllMdiFrom=Function(App:TApplication;mfrmHdl:THandle;Scr:TScreen;Owner_s:Tform):Tform;stdcall;TGetCaption = function: Pchar; StdCall;TGetFormGuid= function: Pchar; StdCall;EdllLoadError=class(Exception);TTestPlugIn=classcaption:string;//加载的getption返加地址Address:THandle;//存取加载的dll的地址call:Pointer;//存取ShowDllForm的句柄guid:string;//窗体的唯一标识end;TMainForm = class(TForm)MainSb: TStatusBar;MainMenu1: TMainMenu;N1: TMenuItem;N_Window: TMenuItem;testForm1: TMenuItem;N2: TMenuItem;N21: TMenuItem;CoolBar1: TCoolBar;ToolBar1: TToolBar;ToolButton3: TToolButton;ToolButton4: TToolButton;ToolButton5: TToolButton;MainTC: TRzTabControl;N_plugins: TMenuItem;procedure FormCreate(Sender: TObject);procedure MainTCChange(Sender: TObject);procedure MainTCClose(Sender: TObject; var AllowClose: Boolean);procedure FormDestroy(Sender: TObject);privateprocedure MainCopyDataMsg(Var Msg : TMessage); Message WM_COPYDATA; //用于进程 或dll中传递 消息publicprocedure tabControl_SelectedIndexChanged(sender:TObject);procedure TabControcl_ChangeTabPage(sender:TObject);procedure AdjustTabControl(Sender:TForm;   Delete:Boolean);procedure TabControl_DeleteTabFromCaption(sCaption:string);//窗体关闭时能过标题关闭窗体//---procedure LoadPlugIns;//加载插件到菜单procedure PlugInsClick(Sender: TObject); //插件菜单点击事件procedure FreePlugIns; //释放插件end;varMainForm: TMainForm;ShowDllFrom:TTestdllMdiFrom;  //声明接口函数数型Plugins:TList;//存放每个Dll加载后的相关信息StopSearch:Boolean;
//  function ShowDllForm( App:TApplication;Scr:TScreen;Owner_s:Tform): Boolean;stdcall; external 'TestDllFrm.dll';//为了简单,使用静态调用方法
implementation{$R *.dfm}
//
//查找文件,并存于Files中
procedure SearchFileExt(const Dir, Ext: string; Files: TStrings);
varFound: TSearchRec;Sub: string;i: Integer;Dirs: TStrings;Finished: Integer;
beginStopSearch := False;Dirs := TStringList.Create;Finished := FindFirst(Dir + '*.*', 63, Found);while (Finished = 0) and not (StopSearch) dobeginif (Found.Name[1] <> '.') thenbeginif (Found.Attr and faDirectory = faDirectory) thenDirs.Add(Dir + Found.Name) //Add to the directories list.elseif Pos(UpperCase(Ext), UpperCase(Found.Name)) > 0 thenFiles.Add(Dir + Found.Name);end;Finished := FindNext(Found);end;FindClose(Found);if not StopSearch thenfor i := 0 to Dirs.Count - 1 doSearchFileExt(Dirs[i], Ext, Files);Dirs.Free;
end;
//-----------------------------------------------------------------
procedure TMainForm.tabControl_SelectedIndexChanged(sender: TObject);
var i:Integer;
beginif   MainForm.MDIChildCount   >0 thenbeginfor i:=0 to MainForm.MDIChildCount-1 dobeginif  MainTC.TabIndex=i thenbeginMainForm.MDIChildren[i].ActiveMDIChild;end;end;  end;
end;procedure TMainForm.FormCreate(Sender: TObject);
beginif MainTC.Tabs.Count=0 thenMainTC.Height:=0elseMainTC.Height:=28;LoadPlugIns;end;procedure TMainForm.MainTCChange(Sender: TObject);
varTabCap:String;I:   Integer;Child:   TForm;
beginif MainTC.Tabs.Count=0 thenbeginMainTC.Height:=0;exit;endelseMainTC.Height:=28;TabCap:=MainTC.Tabs[MainTC.TabIndex].Caption;for   I   :=   MDIChildCount   -   1   downto   0   dobeginChild   :=   MDIChildren[I];if   Child.Caption   =     TabCap   thenChild.Show;end;MainSb.Panels[1].Text:=IntToStr(MainTC.TabIndex);end;procedure TMainForm.TabControcl_ChangeTabPage(sender: TObject);
var i:Integer;
beginif (Self.MDIChildCount>0) and (MainTC.TabIndex>-1) thenbeginfor i:=0 to Self.MDIChildCount-1 dobeginif MainTC.TabIndex=i thenbeginSelf.MDIChildren[i].WindowState:=wsMaximized;Self.MDIChildren[i].Visible:=True;Self.MDIChildren[i].ActiveMDIChild;endelsebeginif Self.MDIChildren[i].Visible thenSelf.MDIChildren[i].Visible:=False;end;  end;  end;  
end;procedure TMainForm.AdjustTabControl(Sender: TForm; Delete: Boolean);
varI:Integer;Found:Boolean;tmp_tab:TRzTabCollectionItem;
begin//查找Found   :=   False;for   I   :=   0   to   MainTC.Tabs.Count   -   1   dobeginif   Sender.Caption   =   MainTC.Tabs[i].Caption   thenbeginFound   :=   True;   //找到if   Delete   then   //删除MainTC.Tabs.Delete(I)else     //激活beginif   MainTC.TabIndex   <>   I   thenMainTC.TabIndex   :=   I;Sender.WindowState:=wsMaximized;  end;break;end;end;if   not   Found   then   //增加并激活begintmp_tab:=TRzTabCollectionItem.Create(MainTC.Tabs);tmp_tab.Caption:=Sender.Caption;tmp_tab.Hint:=IntToStr(Sender.Handle);MainTC.TabIndex   :=   MainTC.Tabs.Count   -   1;end;MainSb.Panels[3].Text :='handle:'+inttostr(MainForm.Handle);
end;procedure TMainForm.MainTCClose(Sender: TObject; var AllowClose: Boolean);
var i:Integer;tmpcaption:string;
begintmpcaption:=MainTC.Tabs.Items[MainTC.TabIndex].Caption   ;for i:=0 to MainForm.MDIChildCount-1 dobeginif MainForm.MDIChildren[i].Caption=  tmpcaption       thenMainForm.MDIChildren[i].Close;end;  
end;procedure TMainForm.MainCopyDataMsg(var Msg: TMessage);
var tmpstr:string;sHead:string;tmpCaption,TMP_frmGuid:string;cdds : TcopyDataStruct;
beginif msg.Msg = WM_COPYDATA thenbegincdds := PcopyDataStruct(Msg.LParam)^;tmpstr := (Pchar(cdds.lpData));sHead:=LeftStr(tmpstr,5);if sHead='XFRM:'  then  //X掉即关闭子窗体begintmpCaption:=RightStr(tmpstr,Length(tmpstr)-5);TabControl_DeleteTabFromCaption(tmpCaption)  ;end;if sHead='FUID:'  then  //根据guid freeFrombeginTMP_frmGuid:=RightStr(tmpstr,Length(tmpstr)-5);// FreePlugIns_fromCapiont(TMP_frmGuid);end;end;
end;procedure TMainForm.TabControl_DeleteTabFromCaption(sCaption:string);
varI:Integer;Found:Boolean;tmp_tab:TRzTabCollectionItem;
begin//查找Found   :=   False;for   I   :=   0   to   MainTC.Tabs.Count   -   1   dobeginif   sCaption   =   MainTC.Tabs[i].Caption   thenbeginFound   :=   True;   //找到MainTC.Tabs.Delete(i);break;end;end;end;procedure TMainForm.LoadPlugIns;
varFiles: TStrings;i: Integer;TestPlugIn: TTestPlugIn;NewMenu: TMenuItem;GetCaption: TGetCaption;fm:TTestdllMdiFrom;GetFormGuid:TGetFormGuid;
beginFiles := TStringList.Create;Plugins := TList.Create;//查找指定目录下的.dll文件,并存于Files对象中SearchFileExt(ExtractFilepath(Application.Exename), '.dll', Files);//加载查找到的DLLfor i := 0 to Files.Count - 1 dobeginTestPlugIn := TTestPlugIn.Create;TestPlugIn.Address := LoadLibrary(PChar(Files[i]));if TestPlugIn.Address = 0 thenraise EDLLLoadError.Create('装载' + PChar(Files[i]) + '失败');try@GetCaption := GetProcAddress(TestPlugIn.Address, 'GetCaption');TestPlugIn.Caption := GetCaption;@fm:=GetProcAddress(TestPlugIn.Address, 'ShowDllForm');TestPlugIn.call:=@fm   ;@GetFormGuid:=GetProcAddress(TestPlugIn.Address,'GetFormGuid') ;TestPlugIn.guid:=GetFormGuid;PlugIns.Add(TestPlugIn);//创建菜单,并将菜单标题,Onclick事件赋值NewMenu := TMenuItem.Create(Self);NewMenu.Caption := TestPlugIn.Caption;NewMenu.OnClick := PlugInsClick;NewMenu.Tag := i;N_plugins.Add(NewMenu); //每次在菜单下新增一个模块菜单exceptraise EDLLLoadError.Create('初始化失败');end;end;Files.Free;
end;procedure TMainForm.FreePlugIns;
vari: Integer;tmpHandl:THandle;
begin//将加载的插件全部释放for i := 0 to PlugIns.Count - 1 dobegintmpHandl:=TTestPlugIn(PlugIns[i]).Address;if tmpHandl<>0 thenFreeLibrary(tmpHandl);end;//释放plugIns对象PlugIns.Free;
end;procedure TMainForm.PlugInsClick(Sender: TObject);
var tmpform:TForm;
tmp_swFrom:TTestdllMdiFrom;
i:Integer;
unit TestUnit;interfaceusesShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTTestForm = class(TForm)Memo1: TMemo;Button1: TButton;procedure FormClose(Sender: TObject; var Action: TCloseAction);procedure Button1Click(Sender: TObject);procedure FormCreate(Sender: TObject);privateprocedure SendKeys(sSend:string);procedure SendParmKeys(sSend:string);//发送运行参数publicend;varTestForm: TTestForm;implementationuses myUnit;{$R *.dfm}procedure TTestForm.FormClose(Sender: TObject; var Action: TCloseAction);
beginSendParmKeys('XFRM:'+self.Caption);SendParmKeys('FUID:'+frm_guid);Action:=caFree;TestForm:=nil;
end;procedure TTestForm.Button1Click(Sender: TObject);
beginSendParmKeys(frm_guid);
end;
procedure TTestForm.SendKeys(sSend:string);
vari:integer;focushld,windowhld:hwnd;threadld:dword;ch: byte;
beginwindowhld:=GetForegroundWindow;//获得前台应用程序的活动窗口的句柄threadld:=GetWindowThreadProcessId(Windowhld,nil);//获取与指定窗口关联在一起的一个进程和线程标识符AttachThreadInput(GetCurrentThreadId,threadld,true);//通常,系统内的每个线程都有自己的输入队列。            ////AttachThreadInput允许线程和进程共享输入队列。         ////连接了线程后,输入焦点、窗口激活、鼠标捕获、键盘状态 ////以及输入队列状态都会进入共享状态                      //Focushld:=getfocus;//获得拥有输入焦点的窗口的句柄AttachThreadInput(GetCurrentThreadId,threadld,false);if focushld = 0 then Exit;//如果没有输入焦点则退出发送过程i := 1;while i <= Length(sSend) do//该过程发送指定字符串(中英文皆可以)beginch := byte(sSend[ i ]);if Windows.IsDBCSLeadByte(ch) thenbeginInc(i);SendMessage(focushld, WM_IME_CHAR, MakeWord(byte(sSend[ i ]), ch), 0);endelseSendMessage(focushld, WM_IME_CHAR, word(ch), 0);Inc(i);end;postmessage(focushld,WM_keydown,13,0);//发送一个虚拟Enter按键
end;
procedure TTestForm.SendParmKeys(sSend: string);
vartmpstr:string;cdds : TCopyDataStruct;
begin
tmpstr:=sSend;
cdds.dwData := 0;
cdds.cbData := length(tmpstr)+1;
cdds.lpData := pchar(tmpstr);
SendMessage(DllMfrmHdl,WM_COPYDATA,0,LongWord(@cdds));end;procedure TTestForm.FormCreate(Sender: TObject);
beginend;end.

fmPointer:Pointer;begin i:= TMenuItem(Sender).Tag; tmp_swFrom:=TTestPlugIn(PlugIns[i]).call;//TTestPlugIn(PlugIns[TMenuItem(Sender).Tag]).Child_Form:= TTestPlugIn(PlugIns[TMenuItem(Sender).Tag]).Call; //执行showDllForm函数 tmpform:=tmp_swFrom(application,Self.Handle,Screen,Self); if Assigned(tmpform) then begin with tmpform do begin WindowState:=wsMaximized; Show;//--改为fORM.ShowModal end; AdjustTabControl( tmpform,False); end;end;procedure TMainForm.FormDestroy(Sender: TObject);begin FreePlugins;end;end.





dll窗体1代码


这篇关于Delphi封装Mdi窗体到Dll并使用插件管理,tabControl制作多页面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/643485

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window