我贴上代码大家一起研究:DLL上的代码: -------------------------------------- function LoadModal(const AParentCom:TWinControl):integer;stdcall; begin try if not assigned(MainFrm) then begin MainFrm := TMainFrm.Create(AParentCom); MainFrm.Parent := AParentCom; end; MainFrm.Show ; MainFrm.BringToFront ; except Result := contResErrSys; exit; end; Result := contResOK; end; 原计划嵌入在application上的一个panel里,不过DLL中的窗体只闪了一下就消失,程序运行没有错误提示。 而且我在DLL调试中确定送入的AParentCom没有问题,因为调试的时候可以正确的读入AParentCom.Name
procedure Create_FormInDllAsChild(AParent: TWinControl); procedure Free_FormInDllAsChild; implementation{$R *.DFM}(*----------------------------------------------------------------------------------*) procedure Create_FormInDllAsChild(AParent: TWinControl); begin if not Assigned(frmFormInDll) then begin frmFormInDll := TfrmFormInDll.CreateParented(AParent.Handle); frmFormInDll.Show; end; end; (*----------------------------------------------------------------------------------*) procedure Free_FormInDllAsChild; begin if Assigned(frmFormInDll) then begin frmFormInDll.Release; frmFormInDll := nil; end; end; (*----------------------------------------------------------------------------------*) procedure TfrmFormInDll.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); Params.Style := Params.Style or WS_CHILD; end; (*----------------------------------------------------------------------------------*) procedure TfrmFormInDll.Loaded; begin inherited; Align := alClient; BorderStyle := bsNone; BorderIcons := []; Position := poDefault; end; (*----------------------------------------------------------------------------------*) procedure TfrmFormInDll.Button1Click(Sender: TObject); begin ShowMessage('Form in a dll created as child to a panel') end;end. ============================================================Create a new project (New Application) and code its form to look like the following form unit============================================================ unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls;type TForm1 = class(TForm) Panel1: TPanel; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; implementation{$R *.DFM}procedure Create_FormInDllAsChild(AParent: TWinControl); external 'FormDLL.dll'; procedure Free_FormInDllAsChild; external 'FormDLL.dll';(*----------------------------------------------------------------------------------*) procedure TForm1.Button1Click(Sender: TObject); begin Create_FormInDllAsChild(Panel1); end; (*----------------------------------------------------------------------------------*) procedure TForm1.Button2Click(Sender: TObject); begin Free_FormInDllAsChild; end;end.============================================================
--------------------------------------
function LoadModal(const AParentCom:TWinControl):integer;stdcall;
begin
try
if not assigned(MainFrm) then
begin
MainFrm := TMainFrm.Create(AParentCom);
MainFrm.Parent := AParentCom;
end;
MainFrm.Show ;
MainFrm.BringToFront ;
except
Result := contResErrSys;
exit;
end;
Result := contResOK;
end;
原计划嵌入在application上的一个panel里,不过DLL中的窗体只闪了一下就消失,程序运行没有错误提示。
而且我在DLL调试中确定送入的AParentCom没有问题,因为调试的时候可以正确的读入AParentCom.Name
============================================================
library FormDLL;uses
SysUtils,
Classes,
Controls,
FormInDll in 'FormInDll.pas' {frmFormInDll};{$R *.RES}exports
Create_FormInDllAsChild,
Free_FormInDllAsChild ;
begin
end.
unit FormInDll;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TfrmFormInDll = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Loaded; override;
public
{ Public declarations }
end;var
frmFormInDll: TfrmFormInDll = nil;
procedure Create_FormInDllAsChild(AParent: TWinControl);
procedure Free_FormInDllAsChild;
implementation{$R *.DFM}(*----------------------------------------------------------------------------------*)
procedure Create_FormInDllAsChild(AParent: TWinControl);
begin
if not Assigned(frmFormInDll) then
begin
frmFormInDll := TfrmFormInDll.CreateParented(AParent.Handle);
frmFormInDll.Show;
end;
end;
(*----------------------------------------------------------------------------------*)
procedure Free_FormInDllAsChild;
begin
if Assigned(frmFormInDll) then
begin
frmFormInDll.Release;
frmFormInDll := nil;
end;
end;
(*----------------------------------------------------------------------------------*)
procedure TfrmFormInDll.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or WS_CHILD;
end;
(*----------------------------------------------------------------------------------*)
procedure TfrmFormInDll.Loaded;
begin
inherited;
Align := alClient;
BorderStyle := bsNone;
BorderIcons := [];
Position := poDefault;
end;
(*----------------------------------------------------------------------------------*)
procedure TfrmFormInDll.Button1Click(Sender: TObject);
begin
ShowMessage('Form in a dll created as child to a panel')
end;end.
============================================================Create a new project (New Application) and code its form
to look like the following form unit============================================================
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.DFM}procedure Create_FormInDllAsChild(AParent: TWinControl); external 'FormDLL.dll';
procedure Free_FormInDllAsChild; external 'FormDLL.dll';(*----------------------------------------------------------------------------------*)
procedure TForm1.Button1Click(Sender: TObject);
begin
Create_FormInDllAsChild(Panel1);
end;
(*----------------------------------------------------------------------------------*)
procedure TForm1.Button2Click(Sender: TObject);
begin
Free_FormInDllAsChild;
end;end.============================================================