程
程云
Unregistered / Unconfirmed
GUEST, unregistred user!
如何从一个DLL中显示一个MDI子窗体?
在一个DLL中写如下程序:
Form设为fsMDIChild
unit DLLFrm;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, Calendar;
type
TDLLForm = class(TForm)
calDllCalendar: TCalendar;
end;
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
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application); { show }
Result := Longint(DLLForm);
DLLForm.Caption := ACaption;
end;
procedure CloseCalendar(AFormRef: Longint);
begin
if AFormRef > 0 then
TDLLForm(AFormRef).Release;
end;
end.
在主程序中写如下方法来调用这个DLL中的窗体
主Form设为fsMDIForm
unit MainFfm;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TMainForm = class(TForm)
btnShowCalendar: TButton;
btnCloseCalendar: TButton;
procedure btnShowCalendarClick(Sender: TObject);
procedure btnCloseCalendarClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FFormRef: TForm;
end;
var
MainForm: TMainForm;
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;
end;
end.
只是不知何故,就在创建子窗体时出错,说是没有发现主MDI窗体。
可我明明已设了。
可能是这个子窗体无法……
在一个DLL中写如下程序:
Form设为fsMDIChild
unit DLLFrm;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, Calendar;
type
TDLLForm = class(TForm)
calDllCalendar: TCalendar;
end;
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
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application); { show }
Result := Longint(DLLForm);
DLLForm.Caption := ACaption;
end;
procedure CloseCalendar(AFormRef: Longint);
begin
if AFormRef > 0 then
TDLLForm(AFormRef).Release;
end;
end.
在主程序中写如下方法来调用这个DLL中的窗体
主Form设为fsMDIForm
unit MainFfm;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TMainForm = class(TForm)
btnShowCalendar: TButton;
btnCloseCalendar: TButton;
procedure btnShowCalendarClick(Sender: TObject);
procedure btnCloseCalendarClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FFormRef: TForm;
end;
var
MainForm: TMainForm;
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;
end;
end.
只是不知何故,就在创建子窗体时出错,说是没有发现主MDI窗体。
可我明明已设了。
可能是这个子窗体无法……