CreateFormInPackage (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

Generally, if we want to create a Form from certain Package, we have to find the class first. This example shows how to create a Form in Package (*.bpl) easily without defining the certain class first.

This example uses section Initialization and Finalization.

Within the Initialization section, we call procedure to create a Form. In this case, we have procedure CreateForm, like the code snippet below:

procedure CreateForm;
begin

// Create instance of class <FormClassName> in Application

<FormName> := <FormClassName>.Create(Application);

end;

Then, in the Finalization section, we call a procedure to destroy a Form. In this case, we have the procedure DestroyForm like the code snippet below:

procedure DestroyForm;
begin

// Validate the instance

if Assigned(<FormName>) then
  begin

  // Destroy the instance if assigned

  <FormName>.Destroy;

  end;

end;

Steps

Please follow these steps:

  1. Create New Package Project (File|New|Package - Delphi).
  2. Set the name of project as "TestPackage".
  3. Add a New VCL Form.
  4. Type the code below.

Code

Package Project

Source Code

unit Unit2;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs;

type
  TForm2 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure CreateForm;
begin

// Create instance of class TForm2 in Application

Form2 := TForm2.Create(Application);
Form2.Show;

end;

procedure DestroyForm;
begin

// Validate the instance

if Assigned(Form2) then
  begin

  // Destroy the instance if assigned

  Form2.Destroy;

  end;

end;

initialization

// Call procedure CreateForm when the package is loaded
CreateForm;

finalization

// Call procedure DestroyForm when the package is unloaded
DestroyForm;

end.

Notes

  1. Specify the package output directory of Package Project as the same directory as the output directory of the VCL Form Application Project.
  2. If you do not want to have the same directory, you can define the package output directory in system path, such as: system32.

VCL Form Application Project

When loading the package, an instance of a form will be created. Here are the code snippets showing how to load the package from the VCL Form Application. If you want to learn more details about Loading Packages, see Loading Packages in an Application.

Source Code

unit Unit1;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btnLoad: TButton;
    btnUnLoad: TButton;
    procedure btnLoadClick(Sender: TObject);
    procedure btnUnLoadClick(Sender: TObject);
  private
    { Private declarations }
    FHandle: THandle;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnLoadClick(Sender: TObject);
begin

// Load Package "TestPackage.bpl"

FHandle := LoadPackage('TestPackage.bpl');

end;

procedure TForm1.btnUnLoadClick(Sender: TObject);
begin

// UnLoad Package by Handle

UnloadPackage(FHandle);

end;

end.

Notes

Set Run with Runtime Package
  1. Do not forget to set Link with Runtime Packages as True (XE2 Project Options). If you do not set it, your application will not able get the handle correctly. In previous versions of Delphi, the option was named Build with runtime packages. See Packages (Options).
  2. Define the value of runtime packages with vcl and rtl only when your application is not complex or depends on certain packages.

Uses

See Also

Personal tools
Assistance