在dll中delphi中封装窗体(实例)

 dll工程

library FormDLL;

{ 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
  SysUtils,
  Classes,
  Forms,
  DLLForm in 'DLLForm.pas' {frmDLL},
  dllFrom2 in 'dllFrom2.pas' {Form2};

{$R *.res}

exports
  SynAPP,ShowForm;


begin
end.

//dll单元文件一

unit DLLForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,iniFiles;

type
  TfrmDLL = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  frmDLL: TfrmDLL;

  procedure SynAPP(App:THandle);stdcall;
  procedure ShowForm;stdcall;

implementation

uses Math,dllFrom2;

{$R *.dfm}

procedure SynAPP(App:THandle );stdcall;
begin
  Application.Handle := App;
end;

procedure ShowForm;stdcall;
begin
  try
    frmDLL := TfrmDLL.Create (Application);
    try
      if frmDLL.ShowModal = idOk then
      begin
        try
          Form2 := TForm2.Create(Application);
          Form2.ShowModal;
        finally
          FreeAndnil(Form2);
        end;
      end;
    finally
      FreeAndNil(frmDLL);
    end;
  except
    on E: Exception do
      MessageDlg ('Error in DLLForm: ' +
        E.Message, mtError, [mbOK], 0);
  end;
end;

procedure TfrmDLL.Button1Click(Sender: TObject);
begin
  self.ModalResult := idOk;
end;

end.

dll单元文件二

unit dllFrom2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,iniFiles, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  sMsg : string;
begin
  with TiniFile.Create(ExtractFilePath(paramstr(0)) + 'config.ini') do
  begin
    try
      sMsg := ReadString('hello','abc','NO');
      ShowMessage(sMsg);
    finally
      free;
    end;
  end;
end;

end.

测试工程

program test;

uses
  Forms,
  testDLLForm in 'testDLLForm.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

//单元文件

unit testDLLForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,iniFiles;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

procedure SynAPP(App:THandle);stdcall;external 'FormDLL.dll'; //这里的
procedure ShowForm;stdcall;external 'FormDLL.dll';

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SynAPP(Application.Handle);
  ShowForm ;
end;

 

procedure TForm1.Button2Click(Sender: TObject);
var
  sMsg : string;
begin
  with TiniFile.Create(ExtractFilePath(paramstr(0)) + 'config.ini') do
  begin
    try
      sMsg := ReadString('hello','abc','NO');
      ShowMessage(sMsg);
    finally
      free;
    end;
  end;
end;

end.

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值