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

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


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

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

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

Delphi 主窗体Panel中嵌入DLL窗体功能详细源码_第1张图片

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

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

DLL中的工程源码:

library dllForms;

{ 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,
  Vcl.Forms,
  dxCore,
  Winapi.Windows,
  FormDialog in 'FormDialog.pas' {frmDialog},
  uLoadDLLForm in '..\uLoadDLLForm.pas',
  FormShow in 'FormShow.pas' {frmShow};

{$R *.res}

procedure DLLEntryPoint(Reason: DWORD);
begin
  case Reason of
    DLL_PROCESS_ATTACH:  dxUnitsLoader.Initialize;
    DLL_PROCESS_DETACH:
    begin
      dxUnitsLoader.Finalize;
    end;
  end;
end;
/// 
/// 弹出式窗体的调用
/// 窗体名称
/// 
procedure ShowDLLForm(AParamStr: String); stdcall;
var
  vForm: TfrmDialog;
begin
  if Sametext('TfrmDialog', AParamStr) then
  begin
    vForm := TfrmDialog.Create(nil);
    try
      vForm.ShowModal;
    finally
      FreeAndNil(vForm);
    end;
  end;
end;
/// 
/// 嵌入式窗体,返回TFormClass类,由主程序管理Form
/// 
function GetForm(ClassName: PChar; ADllParams: pDllParams): TFormClass; stdcall;
begin
  if FindClass(ClassName) = nil then
    Exit;
  Result:=TFormClass(FindClass(ClassName));
end;

exports
  ShowDLLForm, GetForm;

begin
  DLLProc := @DLLEntryPoint;
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.

主窗体调用的代码:

unit FormMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics, cxLookAndFeels,
  cxLookAndFeelPainters, Vcl.Menus,
  Vcl.StdCtrls, cxButtons, Vcl.ExtCtrls,
  uLoadDLLForm;//关键的代码在uLoadDllForm单元中

type
  TfrmMain = class(TForm)
    Label1: TLabel;
    pnlTitle: TPanel;
    btnJinShui: TcxButton;
    btnJiaoYu: TcxButton;
    btnMall: TcxButton;
    btnClose: TcxButton;
    btnJinrong: TcxButton;
    pnlContainer: TPanel;
    procedure btnJinrongClick(Sender: TObject);
    procedure btnJinShuiClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FActiveForm: TForm; //存储当前展示的Form(容器类窗体)
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.btnJinrongClick(Sender: TObject);
var
  AForm: TForm;
begin
  AForm := LoadContainerFormDLL('dllForms.dll', 'TfrmShow', pnlContainer);
  if FActiveForm = nil then
    FActiveForm := AForm
  else if FActiveForm <> AForm then
  begin
    FActiveForm.Hide;
    FActiveForm := AForm;
  end;
end;

procedure TfrmMain.btnJinShuiClick(Sender: TObject);
begin
  //主程序调用,弹出框
  LoadFormDLL('dllForms.dll', 'TfrmDialog');
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  //初始化DLL加载项
  initDll;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  //释放所有资源
  uninitDll;
end;

资源下载地址:http://download.csdn.net/detail/yueyun889/9864663

你可能感兴趣的:(Delphi,delphi)