想请教如何在主程序中调用DLL中的窗体?

hdhello 2003-08-21 12:45:41
我看了一些资料,但主要讲函数的调用,窗体的调用讲得很简单,看不明白(作者用一种太简单了,实在懒得再讲的口吻带了两句),哪位仁兄给我扫一下盲?
...全文
43 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yurenjf 2003-08-29
  • 打赏
  • 举报
回复
给你个例子看看,或许你会明白(是MDI子窗体的调用).这如果会了,那普通窗体的调用就更简单啦!!!

//主程序///////////////////

unit MainUnit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ToolWin, ComCtrls, Menus, ImgList;

type
TMainForm = class(TForm)
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ImageList1: TImageList;
procedure fgf1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }

public
{ Public declarations }
procedure DllCall(Sender: TObject);
end;

T_ProvaChild = procedure (ParentApplication: TApplication; parentform: TForm); stdcall;

var
MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.DllCall(Sender: TObject);
var
DllHandle: THandle;
ProcAddr: FarProc;
ProvaChild: T_ProvaChild;
str,str1,str2 : string;
begin
str := (Sender as TToolButton).Caption ;
str1 := copy(str,1,pos('@',str)-1); // str1:=ProjectDll // DLL文件名
str2 := copy(str,pos('@',str)+1,length(str)); //str2:=ProvaChild

DllHandle := LoadLibrary(PChar(str1));
if DllHandle <> 0 then
begin
ProcAddr := GetProcAddress(DllHandle, PChar(str2));
if Assigned(ProvaChild) then
begin
ProvaChild := ProcAddr;
ProvaChild(Application,Self);
end;
//**** freelibrary(DllHandle);//注意!!!注!!!*************
end;

end;

procedure TMainForm.fgf1Click(Sender: TObject);
begin
//DllCall;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
toolbutton1.caption:= ProjectDll@ProvaChild;
toolbutton1.OnClick := DllCall;
end;

end.



//DLL文件/////////////////////////////////

library ProjectDll;

uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ubasStatusaaRoom in 'ubasStatusaaRoom.pas' {FbasStatusaaRoom};

procedure DLLUnloadProc(Reason: Integer); register;
begin
if Reason = DLL_PROCESS_DETACH then Application:=DllApplication;
end;

procedure ProvaChild(ParentApplication: TApplication; ParentForm: TForm); export; stdcall;
begin
if not Assigned(FbasStatusaaRoom) then
begin
Application := ParentApplication;
FbasStatusaaRoom := TFbasStatusaaRoom.Create(nil);
FbasStatusaaRoom.MyParentForm := ParentForm;
FbasStatusaaRoom.MyParentApplication := ParentApplication;
end;
FbasStatusaaRoom.Show;
end;

exports
ProvaChild;


begin
DllApplication:=Application;
DLLProc := @DLLUnloadProc;
end.



//其中的一个子窗体/////////////////////////

unit ubasStatusaaRoom;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ToolWin, ExtCtrls, DB, DBClient, ImgList, Grids,
DBGridEh,ActiveX;

type
TFbasStatusaaRoom = class(TForm)
Panel1: TPanel;
StatusBar1: TStatusBar;
ToolBar1: TToolBar;
tbfirst: TToolButton;
tbprev: TToolButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
MyParentForm: TForm;
MyParentApplication: TApplication;
end;

var
FbasStatusaaRoom: TFbasStatusaaRoom;


implementation

{$R *.dfm}

procedure TFbasStatusaaRoom.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
action := caFree;
FbasStatusaaRoom := nil ;
end;


end.
Shawphen2000 2003-08-21
  • 打赏
  • 举报
回复
《参透Delphi/Kylix》第654页到第657页讲的很清楚。你可以去书店找到这本书看一下。由于篇幅太长,请恕我无法在这里把那些内容敲出来。

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧