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使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安