Delphi 包的应用英文Word文件下载.docx

上传人:b****1 文档编号:1392453 上传时间:2023-04-30 格式:DOCX 页数:15 大小:41.15KB
下载 相关 举报
Delphi 包的应用英文Word文件下载.docx_第1页
第1页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第2页
第2页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第3页
第3页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第4页
第4页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第5页
第5页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第6页
第6页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第7页
第7页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第8页
第8页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第9页
第9页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第10页
第10页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第11页
第11页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第12页
第12页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第13页
第13页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第14页
第14页 / 共15页
Delphi 包的应用英文Word文件下载.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Delphi 包的应用英文Word文件下载.docx

《Delphi 包的应用英文Word文件下载.docx》由会员分享,可在线阅读,更多相关《Delphi 包的应用英文Word文件下载.docx(15页珍藏版)》请在冰点文库上搜索。

Delphi 包的应用英文Word文件下载.docx

3.DropabuttononForm1andaddthefollowingcodetotheOnClickeventhandler:

4.withTForm2.Create(Application)do

5.begin

6.ShowModal;

7.Free;

end;

8.RemembertoaddUnit2toUnit1'

susesclause.

9.Saveandruntheproject.

Wehavecreatedasimpleapplicationthatshowsaformwithabuttonthatshowsanotherformwhenitisclicked.

ButwhatifwewantedtocreateForm2inareusablemodule?

Theansweris--PACKAGES!

TocreateapackageforForm2wewill:

1.Opentheprojectmanager(View|ProjectManager)

2.Right-clickontheProjectGroupandselect"

AddNewProject..."

3.Select"

Package"

fromthe"

New"

itemslist.

4.YoushouldnowseethePackageeditor.

5.Selectthe"

Contains"

itemandpressthe"

Add"

button.

6.Nowusethe"

Browse..."

buttontoselect"

Unit2.pas."

7.Thepackageshouldnowcontaintheunit"

8.Nowsaveandcompilethepackage.

Thepackageisnowcomplete.Youshouldhaveafilecalled"

package1.bpl"

inyourProjectsBPLdirectory.(TheBPListheBorlandPackageLibrary;

theDCPistheDelphiCompiledPackage--sortofliketheDCUofaPASfile.)

That'

sallthatneedtobedonetothepackage.Wenowneedtocompiletheoriginalapplicationwiththepackageoptionswitchedon.

1.Selecttheproject"

Project1.exe"

fromtheProjectManagerbydouble-clickingonit.

2.Right-clickandselect"

Options..."

(YoucanalsoselectProject|Options...fromthemenu.

3.Selectthe"

Packages"

tab.

4.Checkthe"

Buildwithruntimepackages"

checkbox.

5.Edittheedit-boxinthe"

Runtimepackages"

sectiontoread:

"

Vcl50;

Package1"

andOKtheoptions. 

6.NOTE:

DonotremoveUnit2fromtheapplication.

7.Saveandruntheapplication.

Theapplicationwillrunandbehavejustlikebefore--thedifferencecanbeseeninthefilesize.Project1.exeisnowonly14Kasapposedtotheoriginal293K.IfyouusearesourceexplorertoviewthecontentsoftheEXEandtheBPLyouwillfindthatboththeDFMandthecodeforForm2nowresideinthepackage.

Delphiachievesthisbystaticallylinkinginthepackageatcompiletime.(That'

swhyyoushouldn'

tremovetheunitfromtheEXEproject.)

Justthinkofwhatcanbeachievedbydoingthis:

Onecouldcreatedatamodulesinpackagesandquicklymodifytheirsourceandonlydistributethenewpackagewhenourdata-accessruleshavechanged,likewhenwemovefromBDEbasedconnectivitytoADO.Or,wecouldcreateafromthatshow'

sa"

thisoptionisnotavailableinthisversion"

messageinonepackage,andthenasimilarlynamedformthathasfunctionalityinasame-namedpackage.Wewillthenhavea"

Pro"

and"

Enterprise"

versionofourproductwithoutmucheffort.

Dynamicloadandunloadofpackages

StaticallylinkedDLLsandBPLsworkfineinmostcases,butwhatifwedecidenottodeploytheBPL?

Wewouldgeta"

ThedynamiclinklibraryPackage1.bplcouldnotbefoundinthespecifiedpath..."

errorandourapplicationwouldstopfunctioning.Orwhatif,inourmodularapplication,wewantedtohavenumerousplug-inlikemodules?

WeneedawaytodynamicallylinktotheBPLatruntime.

WithDLLsthisisasimpleprocessofusingtheLoadLibraryfunction.

functionLoadLibrary(

lpLibFileName:

PChar):

HMODULE;

stdcall;

OncetheDLLisloadedwecancallexportedfunctionsandprocedureswithintheDLLbyusingtheGetProcAddressfunction.

functionGetProcAddress(hModule:

lpProcName:

LPCSTR):

FARPROC;

WefinallyunloadthedllbyusingtheFreeLibraryfunction.

functionFreeLibrary(hLibModule:

HMODULE):

BOOL;

InthisexamplewewilldynamicallyloadMicrosoft'

sHtmlHelplibrary:

functionTForm1.ApplicationEvents1Help(

Command:

Word;

Data:

Integer;

varCallHelp:

Boolean):

Boolean;

type

TFNHtmlHelpA=function(hwndCaller:

HWND;

pszFile:

PAnsiChar;

uCommand:

UINT;

dwData:

DWORD):

var

HelpModule:

HModule;

HtmlHelp:

TFNHtmlHelpA;

begin

Result:

=False;

HelpModule:

=LoadLibrary('

HHCTRL.OCX'

);

ifHelpModule<

>

0then

begin

@HtmlHelp:

=GetProcAddress(HelpModule,

'

HtmlHelpA'

if@HtmlHelp<

nilthen

=HtmlHelp(Application.Handle,

PChar(Application.HelpFile),

Command,

Data)<

0;

FreeLibrary(HelpModule);

end;

CallHelp:

DynamicallyloadedBPLs

BPLsarejustassimple.Wellalmost.

WedynamicallyloadthepackagebyusingtheLoadPackagefunction.

functionLoadPackage(constName:

string):

WecreateTPersistentClassoftheclasswewishtoinstantiatebyusingtheGetClassfunction.

functionGetClass(constAClassName:

TPersistentClass;

Instantiateanobjectoftheloadedclassanduseit.

Andwhenwearedone,unloadthepackageusingtheUnloadPackageprocedure.

procedureUnloadPackage(Module:

HMODULE);

Letusgobacktoourexampleandmakeafewchanges:

1.Select"

fromtheprojectmanager.

4.Remove"

edit-boxsectionandOKtheoptions.

5.OnDelphi'

stoolbar,clickonthe"

Removefilefromproject"

6.Select"

Unit2|Form2"

fromthelistandthen"

OK."

7.Nowgotothe"

Unit1.pas"

sourceandremoveUnit2fromitsusesclause.(ThesestepsarerequiredtoremoveanylinktoUnit2andthepackagewewishtoloaddynamically.)

8.GotothesourceofButton1'

sOnClickevent.

9.AddtwovariablesoftypeHModuleandTPersistentClass.

10.var

11.PackageModule:

AClass:

12.LoadthepackagePackage1byusingtheLoadPackagefunction.

PackageModule:

=LoadPackage('

Package1.bpl'

13.CheckthatthePackageModuleisnot0(zero).

14.CreateapersistentclassusingtheGetClassfunction,passingitthenameoftheformwithinthepackageasitsparameter:

AClass:

=GetClass('

TForm2'

15.Ifthepersistentclassisnotnil,createanduseaninstanceoftheclassjustabefore.

16.withTComponentClass(AClass).Create(Application)

17.asTCustomFormdo

18.begin

19.ShowModal;

20.Free;

21.Finally,unloadthepackageusingtheUnloadPackageprocedure:

UnloadPackage(PackageModule);

22.Savetheproject.

HereisthecompletelistingoftheOnClickevent:

procedureTForm1.Button1Click(Sender:

TObject);

PackageModule:

ifPackageModule<

ifAClass<

withTComponentClass(AClass).Create(Application)

asTCustomFormdo

ShowModal;

Free;

Unfortunatelythat'

snottheendofit.

TheproblemisthattheGetClassfunctionrequirestheclasstoberegisteredbeforethefunctioncanfindit.Usuallyformclassesandcomponentclassesthatarereferencedinaformdeclaration(instancevariables)areautomaticallyregisteredwhentheformisloaded.Buttheformisn'

tloadedyet.Sowhereshouldweregistertheclass?

Theanswer:

inthepackage.Eachunitinthepackageisinitializedwhenthepackageisloadedandfinalizedwhenthepackageisunloaded.

Let'

sreturntoourexampleandmakeafewchanges:

1.Double-clickon"

Package1.bpl"

intheprojectmanager;

thiswillactivatethepackageeditor.

2.Clickonthe+symbolnextto"

Unit2"

inthe"

section.Thiswillexpandtheunittree.

3.Double-clickon"

Unit2.pas"

toactivatetheunit'

ssourcecode.

4.Scrolldowntotheendofthefileandaddaninitializationsection.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2