Delphi 主窗体Panel中嵌入DLL窗体功能详细源码

来源:互联网 发布:淘宝视频可以放几分钟 编辑:程序博客网 时间:2024/06/07 17:55

自从我的博客被尘封了五年之久,终于选择在今天以主程序嵌入DLL窗体架构的源码为礼物送给大家,希望资源与大家共享,共祝未来的Delphi能够走的更远。


由于现在技术的日益发达,各种软件的界面日益美化,很多客户光看到原生的Delphi程序界面就会选择抛弃我们的产品。于是我们不得不对界面进行美化,对架构进行重新整理。首先要感谢360、QQ这些软件给我们提供了漂亮界面的基础:一个大标题导航栏+操作区域,简单、适用。

今天我们就以一个Demo来实现这种窗体结构,因为是在我的项目中已经使用,架构相关的代码已经被我整理了一遍,虽然看着简单,却真的费了我好多功夫。

程序界面:(真正使用的时候自己美化吧,这里仅仅是个例子。ps:我用的是Dev的控件)


其实程序的架构很简单:一个Panel做为容器,DLL中封装窗体,并将它停靠在panel中。

代码有点多,粘贴处部分有用的代码:

DLL中的工程源码:

[delphi] view plain copy
  1. library dllForms;  
  2.   
  3. { Important note about DLL memory management: ShareMem must be the 
  4.   first unit in your library's USES clause AND your project's (select 
  5.   Project-View Source) USES clause if your DLL exports any procedures or 
  6.   functions that pass strings as parameters or function results. This 
  7.   applies to all strings passed to and from your DLL--even those that 
  8.   are nested in records and classes. ShareMem is the interface unit to 
  9.   the BORLNDMM.DLL shared memory manager, which must be deployed along 
  10.   with your DLL. To avoid using BORLNDMM.DLL, pass string information 
  11.   using PChar or ShortString parameters. }  
  12.   
  13. uses  
  14.   System.SysUtils,  
  15.   System.Classes,  
  16.   Vcl.Forms,  
  17.   dxCore,  
  18.   Winapi.Windows,  
  19.   FormDialog in 'FormDialog.pas' {frmDialog},  
  20.   uLoadDLLForm in '..\uLoadDLLForm.pas',  
  21.   FormShow in 'FormShow.pas' {frmShow};  
  22.   
  23. {$R *.res}  
  24.   
  25. procedure DLLEntryPoint(Reason: DWORD);  
  26. begin  
  27.   case Reason of  
  28.     DLL_PROCESS_ATTACH:  dxUnitsLoader.Initialize;  
  29.     DLL_PROCESS_DETACH:  
  30.     begin  
  31.       dxUnitsLoader.Finalize;  
  32.     end;  
  33.   end;  
  34. end;  
  35. /// <summary>  
  36. /// 弹出式窗体的调用  
  37. /// <param name="AParamStr">窗体名称</param>  
  38. /// </summary>  
  39. procedure ShowDLLForm(AParamStr: String); stdcall;  
  40. var  
  41.   vForm: TfrmDialog;  
  42. begin  
  43.   if Sametext('TfrmDialog', AParamStr) then  
  44.   begin  
  45.     vForm := TfrmDialog.Create(nil);  
  46.     try  
  47.       vForm.ShowModal;  
  48.     finally  
  49.       FreeAndNil(vForm);  
  50.     end;  
  51.   end;  
  52. end;  
  53. /// <summary>  
  54. /// 嵌入式窗体,返回TFormClass类,由主程序管理Form  
  55. /// </summary>  
  56. function GetForm(ClassName: PChar; ADllParams: pDllParams): TFormClass; stdcall;  
  57. begin  
  58.   if FindClass(ClassName) = nil then  
  59.     Exit;  
  60.   Result:=TFormClass(FindClass(ClassName));  
  61. end;  
  62.   
  63. exports  
  64.   ShowDLLForm, GetForm;  
  65.   
  66. begin  
  67.   DLLProc := @DLLEntryPoint;  
  68.   DLLEntryPoint(DLL_PROCESS_ATTACH);  
  69. end.  

主窗体调用的代码:

[delphi] view plain copy
  1. unit FormMain;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxLookAndFeels,  
  8.   cxLookAndFeelPainters, Vcl.Menus,  
  9.   Vcl.StdCtrls, cxButtons, Vcl.ExtCtrls,  
  10.   uLoadDLLForm;//关键的代码在uLoadDllForm单元中  
  11.   
  12. type  
  13.   TfrmMain = class(TForm)  
  14.     Label1: TLabel;  
  15.     pnlTitle: TPanel;  
  16.     btnJinShui: TcxButton;  
  17.     btnJiaoYu: TcxButton;  
  18.     btnMall: TcxButton;  
  19.     btnClose: TcxButton;  
  20.     btnJinrong: TcxButton;  
  21.     pnlContainer: TPanel;  
  22.     procedure btnJinrongClick(Sender: TObject);  
  23.     procedure btnJinShuiClick(Sender: TObject);  
  24.     procedure FormCreate(Sender: TObject);  
  25.     procedure FormDestroy(Sender: TObject);  
  26.   private  
  27.     { Private declarations }  
  28.     FActiveForm: TForm; //存储当前展示的Form(容器类窗体)  
  29.   public  
  30.     { Public declarations }  
  31.   end;  
  32.   
  33. var  
  34.   frmMain: TfrmMain;  
  35.   
  36. implementation  
  37.   
  38. {$R *.dfm}  
  39.   
  40. procedure TfrmMain.btnJinrongClick(Sender: TObject);  
  41. var  
  42.   AForm: TForm;  
  43. begin  
  44.   AForm := LoadContainerFormDLL('dllForms.dll''TfrmShow', pnlContainer);  
  45.   if FActiveForm = nil then  
  46.     FActiveForm := AForm  
  47.   else if FActiveForm <> AForm then  
  48.   begin  
  49.     FActiveForm.Hide;  
  50.     FActiveForm := AForm;  
  51.   end;  
  52. end;  
  53.   
  54. procedure TfrmMain.btnJinShuiClick(Sender: TObject);  
  55. begin  
  56.   //主程序调用,弹出框  
  57.   LoadFormDLL('dllForms.dll''TfrmDialog');  
  58. end;  
  59.   
  60. procedure TfrmMain.FormCreate(Sender: TObject);  
  61. begin  
  62.   //初始化DLL加载项  
  63.   initDll;  
  64. end;  
  65.   
  66. procedure TfrmMain.FormDestroy(Sender: TObject);  
  67. begin  
  68.   //释放所有资源  
  69.   uninitDll;  
  70. end;  

资源下载地址:http://download.csdn.net/detail/yueyun889/9864663
原创粉丝点击