Week 5 April 23, 2001 Week 5 – ActiveX and Winsock ActiveX – What’s all the hype? • Distributed computing (integrate personal machine to WWW) • It’s a (distributed computing) technology o Controls o Components o Documents • Reusable binary code (component) developed under any language capable of generating ActiveX binaries • Modular and interchangeable ActiveX and acronyms: OLE ActiveX DCOM COM Figure 1: ActiveX technologies Component Object Model (COM) • Heart of reusable component development; language/location/compiler independent • Binary-level specification describing how components communicate (interoperability) • Make applications more dynamic, customizable and flexible • It’s the foundation for ActiveX Distributed COM (DCOM) • COM “with a longer wire”; execute COM components remotely • Collectively solve complex problems using remote machines • Security • Enterprise level building block Object Linking and Embedding (OLE) • Technology allowing applications to work together (e.g. Excel and Word) “containers” • Services: drag, drop and automation (one app controls another) ActiveX • Reusable components that can be “plugged” into VB, VC++ etc • Examples: calendars, spell checker, image editor, Winsock • Natural evolution from OLE • VB 5.0 and 6.0 can create ActiveX controls • VBX (VB extensions) cannot be used in VB 5.0 and 6.0 1 Week 5 April 23, 2001 Example: ActiveX (rather than OLE) automation using Word Objective: Clicking the write command button will save whatever is in the text box, into a Word DOC file called myWord.doc Step 1: Create the GUI Project: wordAutomation Form: frmWordAutomation Form: Name: frmAutomation Caption: Automation Word Example Text box: Name: txtDisplay Command Name: cmdWrite Command Name: cmdExit Step 2: Microsoft Word ActiveX component (NB: assumes Microsoft Office installed previously) Select Project-References 2 Week 5 April 23, 2001 Scroll down, look for and check off “Microsoft Word 9.0 Object Library”, click OK Step 3: Code Programming step 'FILE: frmAutomation 'Demonstrates automation using Word Option Explicit Dim mWord As Word.Application Private Sub cmdExit_Click() End End Sub Private Sub cmdWrite_Click() cmdWrite.Enabled = False Call mWord.Documents.Add mWord.Selection.Shading.Texture = wdTexture22Pt5Percent mWord.Selection.Font.Size = 30 Call mWord.Selection.TypeText(txtInput.Text) Call mWord.Documents(1).SaveAs("C:\00Courses\mem800\week05\vbPrograms\myWord.doc") Call mWord.Quit End S ub Private Sub Form_Initialize() Set mWord = New Word.Application End Sub Private Sub Class_Terminate() Set mWord = Nothing End Sub 3 Week 5 April 23, 2001 Explore: • Open myWord.doc – it has whatever was typed in txtInput.text Note the font size, and shading (Format-Borders and Shading-Shading) value • Change font size: mWord.SelectionFont.Size = 12 • Change shading: mWord.Selection.Shading.Texture = wdTexture40Percent Example: ActiveX DLL • VB allows one to create DLL’s; it isn’t executable but is to be used by other programs Objective: Word’s built-in spellchecker can be recycled for our own stand-alone program Step 1: Start an ActiveX DLL project Save Class as classActiveXDll Save Project as projectActiveXDll Step 2: Code programming 'FILE: classActiveXDll 'ActiveX DLL example Option Explicit Private mWordRef As Word.Application Private Sub Class_Initialize() Set mWordRef = New Word.Application End Sub 'Function returns False if s is spelled incorrectly Public Function SpellCheckWord(ByVal s As String) As Boolean SpellCheckWord = mWordRef.CheckSpelling(s) End Function Private Sub Class_Terminate() Call mWordRef.Quit Set mWordRef = Nothing End Sub 4 Week 5 April 23, 2001 Step 3: Give your DLL an appropriate description View – Project Explorer Click once on Project1 to turn blue, right click and select Project Properties Change Project Name and add Project Description 5 Week 5 April 23, 2001 Step 4: Name your class – click on Class1 and change name to: classActiveXDll Step 5: Compile First make sure that from Project-References, “Microsoft Word 9.0 Object Library” is checked off. Save, then Files-Make projectActiveXDll.dll Step 6: Create a program (EXE file) that uses projectActiveXDll.dll GUI: 6 Week 5 April 23, 2001 Save Project as: spellCheckerDemo Save Form as: spellCheckDemo Form Name: frmSpellCheckDemo Caption: Testing SpellChecker DLL Command Name: cmdCheck Label Name: lblTextInput Caption: Word: Text box Name: txtInput Label: Name: lblDisplay BorderStyle: 1 – Fixed Single Step 7: Include the DLL you created in Step 3 (MEM800: ActiveX Spell Checker Demo) View – Resources Also make sure that “Microsoft Word 9.0 Object Library” is also checked Step 8: Code Programming 'FILE: frmSpellCheckDemo 'Form module to exercise SpellChecker DLL Option Explicit Private mChecker As classActiveXDll ‘ recall name of class 7 Week 5 April 23, 2001 Private Sub cmdCheck_Click() If mChecker.SpellCheckWord(txtInput.Text) Then lblDisplay.Caption = txtInput.Text & " is spelled correctly" Else lblDisplay.Caption = txtInput.Text & " is spelled incorrectly" End If End Sub Private Sub Form_Initialize() Set mChecker = New classActiveXDll ‘ recall name of class End Sub Private Sub Form_Terminate() Set mChecker = Nothing End Sub After saving everything, run your program and test: 8 Week 5 April 23, 2001 Example: Off-the-shelf ActiveX Components • ActiveX is a technology – components can be created in VB, VC++ and other compilers • Why re-invent the wheel? • 3rd party vendors supply ActiveX components e.g. LabView Objective: Utilize a shareware LCD ActiveX component (called axpanell.zip – axpanel.dll) Sample Sources: 1. VB Zone: http://pdhut.www5.50megs.com/vbzone/controls/listindex.htm 2. Download 32: http://www.download32.com/windows/Development/ActiveX/default.htm 3. Winsite http://www.winsite.com/webdev/activex/ 4. Code Guru: http://www.vbxtras.com/categories/gauge+dial+metercomponent.asp 5. Total Shareware: http://www.totalshareware.com/ASP/list_view.asp?catid=10&Page=4 6. Yuris Puteinis LCD panel ActiveX: http://www.geocities.com/SiliconValley/Lab/4340/ Download: axPanel.zip (contains ActiveX component) and panelVb.zip (VB examples) Step 1: With ActiveX component in hand, need to register it. Oftentimes shareware comes with a registration program e.g. RegAX Alternatively: Start-Run regsvr32 drive:\path\filename To un-register: Start-Run regsvr32 /u drive:\path\filename Step 2: Make sure Projects-Register has axPanel 1.0 Type Library checked Make sure Projects-Components has axPanel 1.0 Type Library checked This will result in having the axPanel ActiveX component show on your controls toolbox: 9 Week 5 April 23, 2001 Step 3: Construct your GUI: Step 4: Code programming Private Sub cmdDisplay_Click() myAxPanel.Text = txtInput.Text End Sub That’s it! Oftentimes distributed ActiveX components come with explanations of code programming use. 10
Description: