英文 VC程序中使用脚本引.docx
《英文 VC程序中使用脚本引.docx》由会员分享,可在线阅读,更多相关《英文 VC程序中使用脚本引.docx(10页珍藏版)》请在冰豆网上搜索。
![英文 VC程序中使用脚本引.docx](https://file1.bdocx.com/fileroot1/2022-10/29/46df0d16-aaa8-4f74-a365-d9fe93949dc4/46df0d16-aaa8-4f74-a365-d9fe93949dc41.gif)
英文VC程序中使用脚本引
ActiveXScriptinginMFC
BySteveWampler
LikeMicrosoftInternetExplorer,yourapplicationscansupportVBScript(VisualBasicScriptingEdition)andJScript(JavaScript)withverylittleeffortonyourpart.MicrosofthasdonemostoftheworkbyprovidingVBScriptandJScriptenginesandbydefiningtheActiveXscriptingCOMinterfacesthatenableyoutousetheengineswithinyourapplications.
We'lltakeMicrosoft'sworkonestepfurtherwithanMFC-likeC++classcalledCScriptEnginethatimplementstheActiveXscriptinginterfacesinthecontextofasampleMFCapplication.
YoushouldbefamiliarwithMFCandautomation.Ifnot,readuponbothintheVisualC++onlinedocumentation.
NotethatforActiveXscriptingtoworkinyourMFCapplications,yourVisualC++environmentmustbeversion4.2b.The4.2bupgradepatchisavailablefromMicrosoftatunder"DownloadsandPatches."YoumustalsohavetheVBScriptandJScriptDLLsthatcomewithInternetExplorer.YoucandownloadtheDLLsfromMicrosoftat
WhyActiveXscripting?
--------------------------------------------------------------------------------
"AutomationenablesautomationclientssuchasVisualBasictouseyourapplications.ButwhywouldyouwantyourapplicationtosupportActiveXscriptingaswell?
Theanswerdependsonyourapplication."
--------------------------------------------------------------------------------
AutomationenablesautomationclientssuchasVisualBasictouseyourapplications.ButwhywouldyouwantyourapplicationtosupportActiveXscriptingaswell?
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"Goodbye,World!
"ThescriptthencallstheSayGoodbyesubroutine.Afterrunningthescript,ScriptTestdisplays"Goodbye,World!
"
ScriptTestfromaprogrammer'sperspective
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="Goodbye,World!
"
TheMsgBox()methodtakesonestringparametercontainingthemessagetext.Forexample,theVBScriptstatementDocument.MsgBox("Goodbye,World!
")causestheScriptTesttodisplaythefollowingmessage:
Step2:
creatingandusingascriptingengine.TocreateanduseanActiveXscriptingengine,ScriptTestincludestheclassesCScriptEngineandCScriptTestScriptEngine.
--------------------------------------------------------------------------------
"TheCScriptEngineclassisanMFC-likewrapperaroundanActiveXscriptingengine.It'sgeneralenoughthatyoushouldbeabletoreuseitinotherMFCapplications."
-------------------------------------------------------------------------------
TheCScriptEngineclassisanMFC-likewrapperaroundanActiveXscriptingengine.It'sgeneralenoughthatyoushouldbeabletoreuseitinotherMFCapplications