Creating ActiveX Controls for InTouch Using Microsoft Visual Basic.docx
《Creating ActiveX Controls for InTouch Using Microsoft Visual Basic.docx》由会员分享,可在线阅读,更多相关《Creating ActiveX Controls for InTouch Using Microsoft Visual Basic.docx(18页珍藏版)》请在冰豆网上搜索。
CreatingActiveXControlsforInTouchUsingMicrosoftVisualBasic
CreatingActiveXControlsforInTouchUsingMicrosoftVisualBasic
AllTechNotesandKBCDdocumentsandsoftwareareprovided"asis"withoutwarrantyofanykind.SeetheTermsofUseformoreinformation.
Topic#:
001010
Created:
December6,2001
Introduction
ThisdocumentwillhelpusersunderstandhowtoleverageActiveXtechnologytocreateadd-oncontrolstobeusedinsideInTouch™applications.ManyprogrammingtoolscanbeusedtocreateActiveXcontrols,oneofthemostpopularbeingVisualBasic(VB).ItisasumedthatyouhaveaworkingknowledgeofVisualBasic.
ThefirstpartofthisdocumentpresentssomekeyconceptsinActiveX;thesecondsectionshowshowtoactuallyuseVBtocreateasampleActiveXcontrol.
Note:
Thisdocumentisnotintendedtoexplain,injustafewpages,howActiveXcontrolswork,butrathertogiveanoverviewofActiveXandhowtocreateasimplecontrolinVB.PleaserefertotheMicrosoftVBdocumentationandMSDNforcompletedetails.
PartA-TheoryofActiveXControlsandInTouchActiveXContainer
ActiveXControls-AShortDefinition
Inter-process/Inter-nodecommunicationandinteractionbetweendifferentsoftwarepackagesarethedrivingforcebehindDynamicDataExchange(DDE),ObjectLinkingandEmbedding(OLE),ComponentObjectModel(COM,DCOM,COM+),andActiveX,amongothertechnologies.
AnActiveXcontrolisaCOMobjectthatsupportsacustomizable,programmaticinterfaceusingthemethods,events,andpropertiesexposed.BecauseActiveXControlsarenotexecutableprograms,theymustbeinstantiatedandusedinsideotherapplicationscalledActiveXcontainers.ExamplesofActiveXcontainersincludeInternetExplorer,MicrosoftOffice,andwindowsinInTouchapplications.
ActiveXControlsTheory
Interfaces:
Properties,Methods,andEvents
ActiveXcontainersandActiveXcontrolscommunicateviainterfacesthatthecontrolsexposetotheoutsideworld.AninterfacecanincludeanumberofProperties,Methods,andEvents.
∙Propertiesdefinethebehaviorofthecontrol.Forexample,Color,Font,Size,andDataSourceNamecanbepropertiesexposedbyacontrol.WhenActiveXcontrolsareusedinsideInTouch,Propertiescanbeboundtotagnamesindifferentways,aswillbeshownlater.
∙Methodsarefunctionsexposedbythecontrolthatallowthecontainertotriggeractionsinsidethecontrol.Tocallamethod,referencethenameofthecontrol,thenthenameofthefunction:
MyControl3.Connect,YourControl1.CalAvg(1,2,3),#ThatControl7.DoStuff();(ThislastexampleshowsthepropersyntaxinsideInTouchscripts.)
∙EventsallowanActiveXControltosignaltoitscontainerwhenanEventtakesplaceinsidethecontrol.Forexample,Click,MouseMove,orDoneIt.
OtherconceptsinvolvingActiveXcontrolsincludeIdentification,Encapsulation,PropertyPages,Persistence,andCompatibility.Thesearebrieflyexplainedinthefollowingparagraphs.
GUIDs
ComponentsinMicrosoftWindows,whichincludeActiveXcontrols,havea128-bituniqueidentificationknownastheGloballyUniqueIDentifier(GUID).GUIDsarebasedontheDate,Time,andthePCwheretheyaregenerated(thealgorithmsusedtocreatetheGUIDsweredesignedbytheOpenSoftwareFoundation-OSFDistributedComputingEnvironment-DCE).GUIDsidentifyentriesintheWindowsregistry,suchasActiveXcontrols,classes,Interfaces,andsoon.Forexample,theGUIDfortheCalendarcontrolincludedwithMSOfficeXPisstoredintheregistryas:
HKEY_CLASSES_ROOT\CLSID\{8E27C92B-1264-101C-8A2F-040224009C02}
Encapsulation:
ContainmentandAggregation
ActiveXcontrolscanhaveembeddedActiveXControls.Inthatcase,theoutercontroliscalledaCompositecontrolandtheinnercontrolsareconstituentcontrols.
∙Containmenttakesplacewhenacontrolexistswithinanothercontrol.
∙Aggregation(ordelegation)iswhentheexternalcontrolusestheconstituentcontrol'sinterfaceasitsown.
Itistransparenttotheclient(container)whodoestheactualjob.TheconceptofencapsulationispresentedinthepracticalsectionofthisTechNote.
EncapsulationisimportantbecauseitallowstheuseofActiveXcontrolsinsideInTouchwhentheyexposepropertiesotherthanthebasictypesthataresupportedinInTouch7.x,i.e.,Discrete,Integer,Real,andMessage.Theunsupportedcontrolcanbeusedasaconstituentcontrolforanothercontrolthatlimitsthepropertiestofore-mentionedtypes.
PropertyPages
ActiveXcontrolsandcontainerstypicallyincludewindowsordialogboxestoviewandsettheproperties;thesewindowsarecalledPropertyPages.
InTouch,anActiveXcontainer,hasatabbeddialogboxthatdisplaysuser-definedpropertiesorcontainer-definedproperties;AllcontrolsinsideInTouchhavedefaultpropertiesthatincludeControlName,position(Left,Top,Width,Height),andVisible(inRuntime)asshowninFigure1.
VisualBasic,anotherActiveXcontainer,includesFont,Color,andPictureasdefaultproperties.
Figure1:
DefaultPropertiesintheInTouch7.xActiveXContainer
Persistence
Persistenceallowscomponentstosaveandlaterretrievedata.MostcontainerssupportIPersistPropertyBag,anintrinsicinterfacethatgreatlysimplifiestheprocessofreadingandwritingdataforcontrols.ActiveXcontrolsinVisualBasicincludeanumberofmethods,suchasWritePropertiesandReadPropertiestoinvokethePropertyBagobject.
Forexample,thefollowinglinesaves"Wonderware"tothem_strCompanystringproperty:
PropBag.WriteProperty("Wonderware",mbytMyData)
Compatibilty
AcompiledActiveXcontrolincludesGUIDsforthecontrolitselfandforanyobjectclassesorinterfacesthatitimplements.ContainersusetheclassIDtorequestthecontroltocreateanobject,andthenquerytheobjectfortheinterfaceID(IID);anerroroccursifitisnotfound.
Bydefault,VisualBasicsetsthecompatibilityforActiveXcontrolstoProjectCompatibility,whichmeansnewIDsaregeneratedaftereachrecompilation(seeFigure2).Onceacontrolisreleased,itisrecommendedtoswitchfromProject(orNoCompatibility)toBinaryVersionCompatibility.PleaserefertoMSDNortheVisualBasicdocumentationformoredetails.
OneGoldenruleinCOMisthatyoumustneverchangeanobject’sinterfaceonceithasbeencompiledandreleasedbecauseyoucouldbebreakingapplicationsthatusetheobject.Createnewobjectsifnecessary,andnamethemwithasuffixthatidentifiessubsequentreleases:
MyObject2,MyObject3,andsoon.
Figure2:
DefaultVersionCompatibilityinVBActiveXControlProjects
InTouchasanActiveXContainer
InTouch7.xhandlesActiveXcontrolsasiftheyarewizards.Wiz.iniisafileintheInTouchdirectorythatkeepstrackofwhatwizardshavebeeninstalledinWindowMaker.NotethedifferencebetweenregistrationofanActiveXcontrolinMicrosoftWindows(viaregsvr32)andinstallation.Registrationmakesanycontainercapableofcreatinginstancesofacontrol;installationinWindowMakerallowsthecontroltobeconfigured.HereisanexampleoftheWiz.inientrieswith4ActiveXcontrolsinstalled:
[Group0001]
Count=4
Name=ActiveXControls
Item0001=OCXGOT,1
Item0002=OCXGOT,2
Item0003=OCXGOT,3
Item0004=OCXGOT,4
UnlikewithregularInTouchwizards,theentriesintheWiz.inidonotindicatebynamewhichActiveXcontroliswhich;thatjobisdoneintheOcx.inifile.Ocx.iniindicatesthenameandGUIDfortheinstalledcontrols.Forexample:
[ActiveXControls]
Count=4
Item0001={8BD21D60-EC42-11CE-9E0D-00AA006002F3},MicrosoftForms2.0ToggleButton
Item0002={8BD21D10-EC42-11CE-9E0D-00AA006002F3},MicrosoftForms2.0TextBox
Item0003={EAE50EB0-4A62-11CE-BED6-00AA00611080},MicrosoftForms2.0TabStrip
Item0004={79176FB0-B7F2-11CE-97EF-00AA006D2776},MicrosoftForms2.0SpinButton
Wiz.iniandOcx.iniaretextfilesstoredintheInTouchinstallationdirectoryandapplytoanyInTouchapplication.Incontrast,ITGUID.cfgandITOcx.cfgarebinaryfilesthatareapplication-specific.ThesefileskeeptrackofwhatActiveXcontrolshavebeenused(andhowmanytimes)intheapplication,sothatuniquenamesareguaranteedandanewnamegeneratedwhencreatinganewinstanceofacontrol.
Limitations
DevelopersoftencompareInTouchActiveXcontainerswithothercontainers,suchasMicrosoftInternetExplorerorVisualBasic.Itisveryimportanttounderstandthatnotallcontainersincludethesamefunctionality,andInTouchhasanumberoflimitationsthatmustbeobservedwhencreatingActiveXcontrolsforInTouchapplications:
∙SupportedDataTypes
Onceagain,rememberthatInTouchdoesnotsupportdatatypesotherthanDiscrete(boolean),Integer(32-bitnumbers),Real(floatingpointIEEEnotationwith32bits),andMessage(stringsupto131characters).Unsupporteddatatypesincludevariant,pointers,arrays,structures,andparameterizedproperties.
IfyouareplanningtouseanActiveXcontrolthatexposesproperties,methods,andeventsthatdonotmeettherequirementsforInTouchdatatypes,youneedtocreateanActiveXControlthatencapsulates,or"wraps"theoriginalActiveXcontroland"massages"theunsupportedpropertysothatInTouchcanuseit.Itisveryeasytocreatepropertiesandmethodsforawrapper(external)controlthataremappedtopropertiesandmethodsintheconstituent(internal)control.
ThefollowingisanexampleofawrapperfunctiontoencapsulateanunsupportedfunctionthatusestheVariantdatatype.
'Membervariabledefinedasvariant
Dimm_Prop1AsVariant
...
PublicFunctionWrapperFunction()AsString
DimMyVariantAsVariant
MyVariant=UnsupportedFunction(m_Prop1)
WrapperFunction=MyVariant
EndFunction
PublicFunctionUnsupportedFunction(ByVal