Form in DLL
From Free Pascal wiki
Jump to navigationJump to search
The following application and library demonstrate you how to show a modal form from a DLL library. Tested on win32 with Lazarus 1.7 trunk.
Main application:
program MainApp;
uses
Interfaces, Classes, LCLType, StdCtrls, Forms;
procedure DLLDialog_ShowModal(ParentWindow: HWND); external 'DLLDialog.dll';
type
TMainForm = class(TForm)
public
constructor Create(aOwner: TComponent); override;
procedure OpenDLLDialog(Sender: TObject);
end;
{ TMainForm }
constructor TMainForm.Create(aOwner: TComponent);
var
BtnOpen: TButton;
begin
inherited CreateNew(aOwner);
Position := poWorkAreaCenter;
Width := 400;
Height := 200;
BtnOpen := TButton.Create(Self);
BtnOpen.Parent := Self;
BtnOpen.Caption := 'Show DLL modal form';
BtnOpen.AutoSize := True;
BtnOpen.OnClick := @OpenDLLDialog;
end;
procedure TMainForm.OpenDLLDialog(Sender: TObject);
var
DisabledList: TList;
begin
DisabledList := Screen.DisableForms(nil, nil);
try
try
DLLDialog_ShowModal(Self.Handle);
finally
Screen.EnableForms(DisabledList);
end;
finally
DisabledList.Free;
end;
end;
var
MainForm: TMainForm;
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
DLL with a form:
library DllDialog;
{$mode objfpc}{$H+}
uses
Interfaces, Classes, LCLType, StdCtrls, Forms, Dialogs;
type
TDLLDialog = class(TForm)
protected
procedure CreateParams(var Params: TCreateParams); override;
public
procedure BtnShowMessageClick(Sender: TObject);
constructor Create(aOwner: TComponent); override;
public
ParentFormHandle: HWND;
end;
procedure DLLDialog_ShowModal(ParentWindow: HWND);
var
DLLDialog: TDLLDialog;
begin
DLLDialog := TDLLDialog.Create(nil);
try
DLLDialog.ParentFormHandle := ParentWindow;
DLLDialog.ShowModal;
finally
DLLDialog.Free;
end;
end;
exports
DLLDialog_ShowModal;
{ TDLLDialog }
constructor TDLLDialog.Create(aOwner: TComponent);
var
BtnShowMessage: TButton;
begin
inherited CreateNew(aOwner);
Caption := 'This form is in a DLL !!!';
Position := poWorkAreaCenter;
Width := 200;
Height := 100;
BtnShowMessage := TButton.Create(Self);
BtnShowMessage.Parent := Self;
BtnShowMessage.Caption := 'Show message';
BtnShowMessage.AutoSize := True;
BtnShowMessage.OnClick := @BtnShowMessageClick;
end;
procedure TDLLDialog.BtnShowMessageClick(Sender: TObject);
begin
ShowMessage('Hello from DLL!');
end;
procedure TDLLDialog.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.WndParent := ParentFormHandle;
end;
begin
Application.Initialize;
end.