怎么用DLL实现窗体的重用????具体一点最好.
解决方案 »
- 子目录操作问题(目录树结构).
- 取时间问题,急
- 为什么数据恢复提示错误,无法恢复.高手请进!!!
- 怎样制作可定制的工具栏?
- linzhengqun(风) 进来领分!!!!感谢(5)
- f1book如何去掉底部显示(sheet1)那行
- 我用WebSnap技术开发数据库网页时,用D6能正常显示,但用D7同样的操作步骤却发生Internal Application Error 尚未调用 CoInitialize错误
- Delphi5的程序在Delphi6下重新编译出现错误?如何解决?高手请进????????
- 难死我了!如何编程象spy++那样取得任意窗口对象的handle?
- 用DELPHI开发数据库遇到的删除问题
- 运用EhLib中的dbguideh控件遇到的问题?
- 如何捕获TQuery的错误??
动态调用: Windows API loadlibrary getprocaddress(好象是) freelibrary,调用DLL中的showform方法,当然你要先编好.
Dll,interface,MainApplication;1、先定义一个接口:
*****************************
unit myinterface;interfacetype
ImyTest = interface
['{8068AF74-187F-4847-9A5D-915CB4AF8FC7}']
function F1:integer;
end;
myTestClass = Class(TinterfacedObject,ImyTest)
function F1:integer;virtual;abstract;
end;implementation{ myTestClass }{
function myTestClass.F1: integer;
begin
result := 999;
end;
}end.
*****************************
2、在动态库中对其接口进行实现;
****************************
library Project2;{ 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
SysUtils,
myinterface,
Classes;type
myTest = Class(myTestClass)
function F1:integer;override;
end; {$R *.res}{ myTest }function getMyClass:myTest;
var
my:myTest;
begin
my := myTest.Create();
result := my;
end;
exports
getMyClass;
{ myTest }function myTest.F1: integer;
begin
result := 1000;
end;begin
end.
当然这就是你封在动态库里的窗体,只是将父类改称冲接口继承的类即可;
********************************
3、主程序引用接口,调用动态库中的窗体,调用其中的功能函数;
********************************
unit main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,myinterface;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function getMyClass:myTestClass;external 'dll\Project2.dll';
var
Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
my:ImyTest;
begin
my := getMyClass().Create();
showmessage(inttostr(my.F1));
end;end.
***********************************
这样实现了功能模块与主程序模块的或者其他模块的分离,降低了耦合性;
***********************************
问题的引出
***********************************
通常有些人,想把窗体封装到动态库中,这就涉及到一个问题;
1、如果动态库中的窗体有自己的功能函数,主程序如果想调用,是看不见的,
只有引用动态库中的窗体,这样主程序就和动态库中的窗体的耦合性增加了;
另一方面,动态库的编写者的代码被公开了;容易被修改,导致整体项目的混乱;
2、所以应用接口的方法,实现了两者的分离;程序的整体结构以维护,小组同时工作
的可能性增加;个人观点,仅供参考