Form in a DLL file Delphi tutorials


In this example you can see how to create dll file with form in it , and call this form from host application.



Following is the source code :



(code style formatted by http://hilite.me/ )




FormInDLL.dpr

library FormInDLL;

{ 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
  FastMM4,
  FastShareMem,
  SysUtils,
  Classes,
  Windows,
  Controls,
  Forms,
  formDLL in 'formDLL.pas' {DllForm};

{$R *.res}
procedure Create_Form(pn:HWND);
begin
DllForm:=TDllForm.CreateParented(pn);
DllForm.BorderStyle:=bsNone;
DllForm.Show;
end;

procedure Size_Form(pnh,pnw:integer);
begin
DllForm.Height:=pnh;
DllForm.Width:=pnw;
end;

exports
Create_Form, Size_Form;

begin
end.

formDLL.pas

unit formDLL;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, jpeg, pngimage, GifImage;

type
  TDllForm = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    Image1: TImage;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  DllForm: TDllForm;

implementation

{$R *.dfm}

procedure TDllForm.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute() then
begin
Image1.Picture.LoadFromFile(OpenDialog1.FileName);
end;
end;

procedure TDllForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;

end.


Host Application :

unit MainForm;

interface

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

type
  TfrmMain = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    DLLFormCreated:Boolean;
  public
    { Public declarations }
  end;
procedure Create_Form(pn:HWND); external 'FormInDLL.dll' name 'Create_Form';
procedure Size_Form(pnh,pnw:integer); external 'FormInDLL.dll' name 'Size_Form';

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.Button1Click(Sender: TObject);
begin
Create_Form(Panel1.Handle); // call procedure in DLL, create DLL form in Panel
Size_Form(Panel1.Height,Panel1.Width);// call procedure in DLL
DLLFormCreated:=True;
Button1.Hide;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
DLLFormCreated:=False;
end;

procedure TfrmMain.FormResize(Sender: TObject);
begin
if DLLFormCreated=True then
 Size_Form(Panel1.Height,Panel1.Width);// call procedure in DLL
end;

end.
Share on Google Plus

About Clear SoftX

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.