//DLL
library Func;uses
ShareMem,
SysUtils,
Classes,
Forms,
Windows,
MConnect,
Child2 in 'Child2.pas' {FormChild2};{$R *.RES}procedure CallModule();stdcall;export;
begin
if not Assigned(FormChild2) then
FormChild2 := TFormChild2.Create(Application);
FormChild2.Show;
end;exports
CallModule;begin
end;//MDIChild单元:procedure TFormChild2.N9Click(Sender: TObject);
begin
Close;
end;procedure TFormChild2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;procedure TFormChild2.FormDestroy(Sender: TObject);
begin
FormChild2 := nil;
end;
//主程序:
type
TCallModule = procedure();stdcall;var
FormMain: TFormMain;
LibHandle: HModule;
procedure LoadModule(AModuleName: String);implementation{$R *.DFM}
procedure LoadModule(AModuleName: String);
var
CallModule: TCallModule;
begin
LibHandle := LoadLibrary(PChar(AModuleName));
if LibHandle = 0 then Exit;
@CallModule := GetProcAddress(LibHandle,'CallModule');
if @CallModule <> nil then
CallModule();
end;procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
iCount: Integer;
begin
if MessageDLG('是否退出?',mtConfirmation,[mbYes,mbNO],0) = mrYes then
begin
for iCount := 0 to MDIChildCount - 1 do
MDIChildren[iCount].Close;
CanClose := True;
if not (LibHandle = 0) then
FreeLibrary(LibHandle);
end else
CanClose := False;
end;关键是无论DLL还是EXE都要带运行时间包VCL50.bpl编译
这是一个调用DLL中MDICHILDFORM的例子,改成其他窗体类型,一样能用
library Func;uses
ShareMem,
SysUtils,
Classes,
Forms,
Windows,
MConnect,
Child2 in 'Child2.pas' {FormChild2};{$R *.RES}procedure CallModule();stdcall;export;
begin
if not Assigned(FormChild2) then
FormChild2 := TFormChild2.Create(Application);
FormChild2.Show;
end;exports
CallModule;begin
end;//MDIChild单元:procedure TFormChild2.N9Click(Sender: TObject);
begin
Close;
end;procedure TFormChild2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;procedure TFormChild2.FormDestroy(Sender: TObject);
begin
FormChild2 := nil;
end;
//主程序:
type
TCallModule = procedure();stdcall;var
FormMain: TFormMain;
LibHandle: HModule;
procedure LoadModule(AModuleName: String);implementation{$R *.DFM}
procedure LoadModule(AModuleName: String);
var
CallModule: TCallModule;
begin
LibHandle := LoadLibrary(PChar(AModuleName));
if LibHandle = 0 then Exit;
@CallModule := GetProcAddress(LibHandle,'CallModule');
if @CallModule <> nil then
CallModule();
end;procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
iCount: Integer;
begin
if MessageDLG('是否退出?',mtConfirmation,[mbYes,mbNO],0) = mrYes then
begin
for iCount := 0 to MDIChildCount - 1 do
MDIChildren[iCount].Close;
CanClose := True;
if not (LibHandle = 0) then
FreeLibrary(LibHandle);
end else
CanClose := False;
end;关键是无论DLL还是EXE都要带运行时间包VCL50.bpl编译
这是一个调用DLL中MDICHILDFORM的例子,改成其他窗体类型,一样能用
解决方案 »
- 用Dbgrideh自动列宽
- 如何将Excel表中的某一列隐藏。
- combobox的一个小问题
- 如何重复播放一个mp3文件?
- 请教关于sqlserver的存储过程中返回参数的问题
- 控件设成了透明,但是如何让它在移动时能显示下面的控件和背景?
- 什么控件象tmemo一样,而且能够设置 一行的背景色 和 前景色
- 那位大虾知道怎样把参考分转位可用分,我没可用分给你们了
- Delphi6 !!!
- 如何判断一个Database是否在事务处理之中?
- 如何用copyfile函数恢复数据库文件?是不是和备份时一样?只不过目标文件与原文件和备份时相反?
- 数据库文件的恢复是不是备份时的逆过程,只需要用copyfile把备份的数据库copy回去就可以了,拜托相告
你可以在DLL中加入一个窗体,然后写一个过程用来创建这个窗体并显示它,
然后在就可以调用DLL中的这个过程了,它会显示DLL中的窗体了。
《delphi5开发人员指南》中的例子:// Dll library CalendarLib;uses
ShareMem,
SysUtils,
Classes,
DLLFrm in 'DLLFrm.pas' {DLLForm};exports
ShowCalendar;
begin
end.// Dll 中的窗体单元,有一TCalendar控件unit DLLFrm;interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, Calendar;type TDLLForm = class(TForm)
calDllCalendar: TCalendar;
procedure calDllCalendarDblClick(Sender: TObject);
end;{ Declare the export function }
function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;implementation
{$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
var
DLLForm: TDllForm;
begin
// Copy application handle to DLL's TApplication object
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application);
try
DLLForm.Caption := ACaption;
DLLForm.ShowModal;
Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result
finally
DLLForm.Free;
end;
end;procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);
begin
Close;
end;end.
// Projectprogram CalendarTest;uses
Forms,
MainFfm in 'MainFfm.pas' {MainForm};{$R *.RES}begin
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
// Project 中的单元unit MainFfm;interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;type
{ First, define a procedural data type, this should reflect the
procedure that is exported from the DLL. }
TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall; { Create a new exception class to reflect a failed DLL load }
EDLLLoadError = class(Exception); TMainForm = class(TForm)
lblDate: TLabel;
btnGetCalendar: TButton;
procedure btnGetCalendarClick(Sender: TObject);
end;var
MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.btnGetCalendarClick(Sender: TObject);
var
LibHandle : THandle;
ShowCalendar: TShowCalendar;
begin { Attempt to load the DLL }
LibHandle := LoadLibrary('CALENDARLIB.DLL');
try
{ If the load failed, LibHandle will be zero.
If this occurs, raise an exception. }
if LibHandle = 0 then
raise EDLLLoadError.Create('Unable to Load DLL');
{ If the code makes it here, the DLL loaded successfully, now obtain
the link to the DLL's exported function so that it can be called. }
@ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');
{ If the function is imported successfully, then set lblDate.Caption to reflect
the returned date from the function. Otherwise, show the return raise
an exception. }
if not (@ShowCalendar = nil) then
lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
else
RaiseLastWin32Error;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
end;end.
//dll
library CalendarMLLib;uses
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.
//调用function ShowCalendar(AHandle: THandle; ACaption: String): Longint; StdCall;
external 'CALENDARMLLIB.DLL';procedure CloseCalendar(AFormRef: Longint); stdcall;
external 'CALENDARMLLIB.DLL';implementation{$R *.DFM}procedure TMainForm.btnShowCalendarClick(Sender: TObject);
begin
if not Assigned(FFormRef) then
FFormRef := TForm(ShowCalendar(Application.Handle, Caption));
end;procedure TMainForm.btnCloseCalendarClick(Sender: TObject);
begin
if Assigned(FFormRef) then
begin
CloseCalendar(Longint(FFormRef));
FFormRef := nil;
end;
end;procedure TMainForm.FormCreate(Sender: TObject);
begin
FFormRef := nil; // Initialize the FFormRef field to nil.
end;
end.
aboutunit in 'aboutunit.pas' {aboutform};
exports
showform;beginend.
//formunit aboutunit;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
Taboutform = class(TForm)
Label1: TLabel;
Label2: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;var
aboutform: Taboutform;function showform:boolean;stdcall;implementation{$R *.DFM}function showform(title:string;version:string):boolean;
var
aboutform:Taboutform;
begin
result:=false;
aboutform:=taboutform.Create(application);
try
with aboutform do
begin
aboutform.Label1.caption:=title;
aboutform.llabel2.caption:=version;
aboutform.showmodal;
result:=true;
end;
finally
aboutform.free;
end;
end;end.然后在别的程序中调用showform就可以了