使用DLL文件中封装的窗口[2]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 yousoft 的 blog

三、mdi-child在dll中载入并使用
    如果是mdi-child又如何在dll中载入并使用呢,下面就这个问题说说使用dll文件中封装的窗口。
新建一个dll工程,保存为mdidll,再新建一个窗体,formstyle设为fsmdichild,如下:
object form1: tform1
left = 192
top = 133
width = 344
height = 234
caption = 'mdi'
color = clbtnface
font.charset = default_charset
font.color = clwindowtext
font.height = -11
font.name = 'ms sans serif'
font.style = []
formstyle = fsmdichild
oldcreateorder = false
position = podefault
visible = true
onclose = formclose
pixelsperinch = 96
textheight = 13
end

代码如下:

unit mdi_unit;

interface

uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs;

type
tform1 = class(tform)
procedure formclose(sender: tobject; var action: tcloseaction);
private
{ private declarations }
public
{ public declarations }
myparentform: tform;
myparentapplication: tapplication;
end;

var
dllapplication: tapplication;
implementation

{$r *.dfm}

procedure tform1.formclose(sender: tobject; var action: tcloseaction);
begin
action:=cafree;
end;

end.


library mdidll;

uses
windows,
messages,
sysutils,
classes,
graphics,
controls,
forms,
dialogs,
mdi_unit in 'mdi_unit.pas' {form1};

procedure loadchild(parentapplication: tapplication; parentform: tform); export; stdcall;
var
form1: tform1;
dllproc: pointer;

begin
application:=parentapplication;
form1:=tform1.create(parentform);
form1.myparentform:=parentform;
form1.myparentapplication:=parentapplication;
form1.show;
end;

procedure dllunloadproc(reason: integer); register;
begin
if reason = dll_process_detach then application:=dllapplication;
end;

{$r *.res}
exports
loadchild;
begin
dllapplication:=application;
dllproc := @dllunloadproc;
end.

编译后生成mdidll.dll文件。
使用dll中的mdi-child窗口如下:

    新建一个工程,主窗口设置如下formstyle设为fsmdiform:
object form1: tform1
left = 192
top = 133
width = 544
height = 375
caption = 'form1'
color = clbtnface
font.charset = default_charset
font.color = clwindowtext
font.height = -11
font.name = 'ms sans serif'
font.style = []
formstyle = fsmdiform
menu = mainmenu1
oldcreateorder = false
pixelsperinch = 96
textheight = 13
object mainmenu1: tmainmenu
left = 72
top = 136
object n1: tmenuitem
caption = #26032#24314'(&n)'
onclick = n1click
end
end
end
代码:

unit usemdi_unit;

interface

uses
windows, messages, sysutils, variants, classes, graphics, controls, forms,
dialogs, menus;

type
tform1 = class(tform)
mainmenu1: tmainmenu;
n1: tmenuitem;
procedure n1click(sender: tobject);
private
{ private declarations }
public
{ public declarations }
end;
t_provachild = procedure (parentapplication: tapplication; parentform: tform); stdcall;
var
form1: tform1;

implementation

{$r *.dfm}

procedure tform1.n1click(sender: tobject);
var
dllhandle: thandle;
procaddr: farproc;
provachild: t_provachild;
begin
dllhandle := loadlibrary('mdidll');
procaddr := getprocaddress(dllhandle, 'loadchild');
if procaddr <> nil then
begin
provachild := procaddr;
provachild(application,self);
end;
end;

end.

本文关键:DLL,窗口,窗体,封装
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top