我想在DLL中动态生成Form,以下是我的程序代码;我知道友问题只是想让大家给我
一个好的解决方案;
达到功能:从数据库中得到Form的名称(string类型,由调用程式传入),然后再DLL中动态建立。
library MasterData;{ 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
ADODB,SysUtils,Forms,
Classes,
UFrmCode in 'UFrmCode.pas' {FrmCode};
UFrmCode1 in 'UFrmCode1.pas' {FrmCode};
UFrmCode2 in 'UFrmCode2.pas' {FrmCode};
UFrmCode3 in 'UFrmCode3.pas' {FrmCode};
{$R *.res}Function GetClassName(cClassName:string):TForm; //±o¨ì¦W¦r¬°cClassNameªºÃþ¦W
var
rClass:TClass;
begin
rClass:=GetClass(cClassName);
if rClass <> nil then
Application.CreateForm(TCommponentClass(rClass),Result);
end;Procedure Showform(Handle:THandle;FrmName:string);stdcall;
var
FormAll:TForm;
Begin
Application.Handle := Handle;
FormAll:=GetClassName(FrmName);
FormAll.Show;
End;
exports
showform;
begininitialization //加入到这里是错的end.
一个好的解决方案;
达到功能:从数据库中得到Form的名称(string类型,由调用程式传入),然后再DLL中动态建立。
library MasterData;{ 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
ADODB,SysUtils,Forms,
Classes,
UFrmCode in 'UFrmCode.pas' {FrmCode};
UFrmCode1 in 'UFrmCode1.pas' {FrmCode};
UFrmCode2 in 'UFrmCode2.pas' {FrmCode};
UFrmCode3 in 'UFrmCode3.pas' {FrmCode};
{$R *.res}Function GetClassName(cClassName:string):TForm; //±o¨ì¦W¦r¬°cClassNameªºÃþ¦W
var
rClass:TClass;
begin
rClass:=GetClass(cClassName);
if rClass <> nil then
Application.CreateForm(TCommponentClass(rClass),Result);
end;Procedure Showform(Handle:THandle;FrmName:string);stdcall;
var
FormAll:TForm;
Begin
Application.Handle := Handle;
FormAll:=GetClassName(FrmName);
FormAll.Show;
End;
exports
showform;
begininitialization //加入到这里是错的end.
ShareMem,
SysUtils,
Classes,
DLLFrm in 'DLLFrm.pas' {DLLForm};exports
ShowCalendar,
CloseCalendar;
begin
end.
unit DLLFrm;interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, Calendar;type TDLLForm = class(TForm)
calDllCalendar: TCalendar;
end;{ Declare the export function }
function ShowCalendar(AHandle: THandle; ACaption: String): Longint; stdCall;
procedure CloseCalendar(AFormRef: Longint); stdcall;
implementation
{$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): Longint;
var
DLLForm: TDllForm;
begin
// Copy application handle to DLL's TApplication object
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application);
Result := Longint(DLLForm);
DLLForm.Caption := ACaption;
DLLForm.Show;
end;procedure CloseCalendar(AFormRef: Longint);
begin
if AFormRef > 0 then
TDLLForm(AFormRef).Release;
end;end.开发人员指南中的!
或者到Torry.net上下载Library Manager控件(100%源程序)!!!