英文 VC程序中使用脚本引Word格式.docx
《英文 VC程序中使用脚本引Word格式.docx》由会员分享,可在线阅读,更多相关《英文 VC程序中使用脚本引Word格式.docx(10页珍藏版)》请在冰豆网上搜索。
sworkonestepfurtherwithanMFC-likeC++classcalledCScriptEnginethatimplementstheActiveXscriptinginterfacesinthecontextofasampleMFCapplication.
YoushouldbefamiliarwithMFCandautomation.Ifnot,readuponbothintheVisualC++onlinedocumentation.
NotethatforActiveXscriptingtoworkinyourMFCapplications,yourVisualC++environmentmustbeversion4.2b.The4.2bupgradepatchisavailablefromMicrosoftatunder"
DownloadsandPatches."
YoumustalsohavetheVBScriptandJScriptDLLsthatcomewithInternetExplorer.YoucandownloadtheDLLsfromMicrosoftat
WhyActiveXscripting?
--------------------------------------------------------------------------------
"
AutomationenablesautomationclientssuchasVisualBasictouseyourapplications.ButwhywouldyouwantyourapplicationtosupportActiveXscriptingaswell?
Theanswerdependsonyourapplication."
Theanswerdependsonyourapplication.
Forexample,ifyourapplicationisanActiveXcontrolcontaineryouruserswilllikelywanttowritescriptsthataccessthecontrols'
automationevents,properties,andmethods.Yourusersmayalsowanttheirscriptsstoredwithintheapplication'
sdocuments.Andfinally,theylikelywantachoiceinscriptinglanguages.ByaddingsupportforActiveXscripting,yourapplicationscandoallofthesethings.Let'
sexamineasampleapplicationcalledScriptTesttoseehow.
TheScriptTestapplicationfromtheuser'
sperspective
ScriptTestisaHello,World!
–typeapplication.It'
sasimpleSingleDocumentInterface(SDI)applicationgeneratedusingtheMFCAppWizard.Here'
swhatScriptTestlookslikeinaction:
ToeditandrunaScriptTestscript,youinvoketheEnterScriptdialogbyselectingRunfromtheScriptmenu.Here'
swhattheEnterScriptdialoglookslike:
IntheScriptTypebox,youchooseVBScriptorJScript.Youenterthescript'
stextintheScripteditcontrol.
Togiveitatry,downloadtheScriptTestapplicationfromtheVCDJweb-siteandrunit.Fromthemenubar,selectScriptandRun,andenterthefollowingVBScript:
SubSayGoodbye
Document.Text="
Goodbye,World!
EndSub
SayGoodbye
PressOKtorunthescript.
ThisVBScriptdeclaresaSayGoodbyesubroutinethatsimplysetsthedocument'
sTextproperty(describedshortly)to"
ThescriptthencallstheSayGoodbyesubroutine.Afterrunningthescript,ScriptTestdisplays"
ScriptTestfromaprogrammer'
WeenabledActiveXscriptinginScriptTestintwosteps:
first,weaddanautomationmethod,andapropertyinScriptTest'
sdocumentclass,CScriptTestDoc,gaveascriptsomethingtodo;
second,twoC++classesenabledScriptTesttocreateanduseanActiveXscriptingengine.
Step1:
automatingtheScriptTestapplication.BeforeaddingsupportforActiveXscripting,weusetheVisualC++ClassWizardtoaddanautomationpropertycalledTextandamethodcalledMsgBox()toScriptTest'
sCScriptTestDocclass.TheTextpropertyisheldinaCStringvariablewithinScriptTest'
sdocumentclass.ItsvalueiswhatyouseewritteninScriptTest'
swindow.
InVBScript,youcangetandsetthedocument'
sTextpropertylikeso:
'
gettheTextproperty
strText=Document.Text
settheTextproperty
Document.Text="
TheMsgBox()methodtakesonestringparametercontainingthemessagetext.Forexample,theVBScriptstatementDocument.MsgBox("
)causestheScriptTesttodisplaythefollowingmessage:
Step2:
creatingandusingascriptingengine.TocreateanduseanActiveXscriptingengine,ScriptTestincludestheclassesCScriptEngineandCScriptTestScriptEngine.
TheCScriptEngineclassisanMFC-likewrapperaroundanActiveXscriptingengine.It'
sgeneralenoughthatyoushouldbeabletoreuseitinotherMFCapplications."
-------------------------------------------------------------------------------
sgeneralenoughthatyoushouldbeabletoreuseitinotherMFCapplications