A lot of DLLs with forms, Load dynamically into a PageControl of a Main-Form

20次阅读
没有评论

A lot of DLLs with forms, Load dynamically into a PageControl of a Main-Form

http://delphi.cjcsoft.net//viewthread.php?tid=46037

Title: A lot of DLLs with forms, Load dynamically into a PageControl of a Main-Form

Question: How to put DLL-Forms as Parent to a MainForm-PageControl-TabSheet?

Answer:
I know, there are a lot of samples from DLLs, but I don’t found any one describing how to load DLLs with Forms into a Main form with a PageControl. Each DLL opens an own TTabSheet! Isn’t it nice to see how it works?

====The DLL No. 1: =====(make so much you want like this)=========

unit uDLL;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TDLLForm = class(TForm)
Label1: TLabel;
Edit1: TEdit;
ComboBox1: TComboBox;
ListBox1: TListBox;
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
function Initialisation:HWND; stdcall; forward;
function CloseDLL:Boolean; stdcall; forward;
var
DLLForm: TDLLForm;
implementation
{$R *.DFM}
function Initialisation:HWND;
begin
DLLForm:=TDLLForm.Create(nil);
DLLForm.Caption:='TestDLL';
result:=DLLForm.Handle;
end;
function CloseDLL:Boolean;
begin
DLLForm.Release;
result:=true;
end;
end.

===========The EXE ========================================

unit uMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, ExtCtrls;
type
TInitialisation = function:HWND;
TCloseDLL=function:Boolean;
TTab = class(TTabSheet)
private
CloseDLL:TCloseDLL;
LibDLL:THandle;
WinHandle:HWND;
end;
TForm1 = class(TForm)
PageControl1: TPageControl;
Panel1: TPanel;
BtnSearchDLL: TButton;
BtnClose: TButton;
procedure BtnSearchDLLClick(Sender: TObject);
procedure BtnCloseClick(Sender: TObject);
private
procedure adviceDLL(Name:String);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.adviceDLL(Name:String);
var
hndDLLHandle: THandle;
Initialisation:TInitialisation;
CloseDLL:TCloseDLL;
Tab:TTab;
WinHndl:HWND;
WinName: Array [0..255] of char;
begin
try
// load dll in dinamic type(mode)
hndDLLHandle := loadLibrary(PChar(Name));
if hndDLLHandle 0 then begin
// get function address
@Initialisation:=getProcAddress(hndDLLHandle,'Initialisation');
@CloseDLL:=getProcAddress(hndDLLHandle,'CloseDLL');
// if function address exists
if (addr(Initialisation)nil) AND (addr(CloseDLL)nil) then begin
Tab:=TTab.Create(nil);
Tab.PageControl:=PageControl1;
WinHndl:=Initialisation;
Windows.SetParent(WinHndl,Tab.handle);
windows.ShowWindow(WinHndl,SW_SHOWMAXIMIZED);
GetWindowText(WinHndl,WinName,255);
Tab.Caption:=WinName;
Tab.CloseDLL:=CloseDLL;
Tab.LibDLL:=hndDLLHandle;
Tab.WinHandle:=WinHndl;
end;
end;
except
on E:Exception do ShowMessage(E.Message);
end;
end;
procedure TForm1.BtnSearchDLLClick(Sender: TObject);
var
sr: TSearchRec;
begin
if FindFirst('Test*.DLL',faAnyFile,sr) = 0 then begin
adviceDLL(sr.Name);
while FindNext(sr) = 0 do adviceDLL(sr.Name);
FindClose(sr);
end;
end;
procedure TForm1.BtnCloseClick(Sender: TObject);
var
Tab:TTab;
CloseDLL:TCloseDLL;
begin
while PageControl1.PageCount0 do begin
Tab:=TTab(PageControl1.Pages[0]);
CloseDLL:=Tab.CloseDLL;
Tab.PageControl:=nil;
Tab.Free;
CloseDLL;
end;
end;
end.

来自墙外,还没测试

正文完