Windows脚本编程核心技术精解Chapter19.pdf
《Windows脚本编程核心技术精解Chapter19.pdf》由会员分享,可在线阅读,更多相关《Windows脚本编程核心技术精解Chapter19.pdf(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 19Using the WebBrowser ControlIn This Chapter?Discover the versatile WebBrowsercontrol,and create your own dialog boxesusing plain HTML templates?Enumerate and access DHTML elements?Link script procedures to DHTML buttons using GetRef,and bypass InternetExplorer security dialogs?Control your
2、 WebBrowserform window through HTML commands?Validate HTML form input,and color-code missing form fields?Let your COM object respond to events fired by the WebBrowsercontrol,and disable the context menu this way?Use radio buttons and determine which radio button was selected?Customize your HTML dial
3、og container,change caption text,and disablesizingThe WebBrowsercontrol is the heart of Internet Explorer.It candisplay HTML documents and is a very versatile design element youcan incorporate into your own dialog boxes.In this chapter,learn howto use the WebBrowsercontrol to create fully configurab
4、le HTML-basedcustom dialog boxes and transfer the results back into your scripts.The WebBrowser Control:Mastering Real-World TasksMicrosoft has stuffed all kinds of controls into Windows,and while most s of them are busy working for the operating system,your scripts can accessthose components,too.On
5、e of the most versatile controls is the WebBrowsercontrol.It works insideof any Internet Explorer window,and because Microsoft keeps expanding therole HTML plays,youll see this control in many Windows 2000 system dialogboxes,too.The WebBrowsercontrol is actually a miniature Web browser.It containsev
6、erything necessary to load and display HTML content.4684-8 ch19.f.qc 3/3/00 9:42 AM Page 539Actually,Internet Explorer is just a wrapping around the WebBrowsercontrol.The Internet Explorer application provides the toolbars and other gadgetshelpful to navigate the Web,but the true power comes from th
7、e WebBrowsercontrol embedded in Internet Explorer.The WebBrowsercontrol is by no means limited to displaying Web pages.InChapter 5,I showed you how to use Internet Explorer as a versatile ScriptingHost output window.Now its time to take a close look at the real heart ofInternet Explorer.The WebBrows
8、ercontrol is the perfect foundation for trulycustomizable dialog boxes.It makes much more sense to use the WebBrowsercontrol directly instead of accessing it through Internet Explorer and payingthe price for all of the overhead involved.Creating custom dialog boxesTheres a definite need for custom d
9、ialog boxes.The built-in set of dialogboxes accessible to the Scripting Host is very limited,and its impractical to create COM objects for any custom dialog box you may need.A much more versatile approach are HTML dialog boxes.You can createthem with your favorite HTML editor in a matter of minutes,
10、and its easy to design these dialog boxes just as you can design a Web page.All you need is a way to display your HTML dialog boxes.Displaying is easyjust feed the HTML template to the WebBrowsercontrol.In addition,youllneed some extra tricks to be able to retrieve the results typed into your dialog
11、 box.Ive designed a COM object that uses the WebBrowsercontrol and includesmany fascinating tricks for interacting with the user.Install the COM objectinstallwebdialogsetup.exe.Full source code is provided atcomponentswebdialogwebdialog.vbp.Designing your form:Creating an HTML templateThe first thin
12、g needed is an HTML template.This template defines theelements of your dialog box.Heres a simple example.Type it into yourfavorite text editor and save it as TEMPLATE.HTMin the same folder you are going to use for the sample scripts:p font:12pt ArialYour name:540Part V:Accessing Hidden ComponentsII4
13、684-8 ch19.f.qc 3/3/00 9:43 AM Page 540Next,open the template file.Internet Explorer displays the form,and you cancheck whether everything looks the way you planned.Displaying the template dialog boxNow you are ready to display the form template with your new custom dialogCOM object(see Figure 19-1)
14、.Make sure the following sample script is stored in the same folder you storedyour HTML template!19-1.VBSset tool=CreateObject(“Web.dialog”)current pathmyname=WScript.ScriptFullNamecp=left(myname,InstrRev(myname,“”)templatetemplate=cp&“template.htm”size dialogtool.Width=400tool.Height=300tool.Title=
15、“My Form”tool.SetIcon“shell32.dll”,3tool.Resizable=true display dialog and return object referenceset webcontrol=tool.ShowDialog(template)MsgBox TypeName(webcontrol)Figure 19-1:Use an HTML template to design your own dialog box.The script creates a dialog box and displays your HTML template.Once you
16、click OK,the COM object returns a reference to the element collection.Youllneed this reference in a minute.First,take a look at your options in Table 19-1.Chapter 19:Using the WebBrowser Control541II4684-8 ch19.f.qc 3/3/00 9:43 AM Page 541Table 19-1Properties and Methods to Control the WebBrowser Di
17、alog BoxMethod/PropertyDescriptionProperty CancelButton As StringCaption name of Cancel button.Function DisplayDialog As ObjectShows dialog box and returnselement collection.Property Height As LongHeight of dialog box in pixels.Function LoadDialog(ByVal template Loads dialog template and returns As
18、String)As Objectelement collection.Property OKButton As StringCaption name of OK button.Property Resizable As BooleanTrue:Dialog box is resizable.Sub SetIcon(ByVal iconlib Sets the dialog box icon.Supply As String,ByVal index As Long)name of icon file and icon index.Sub ShowButtons(ByVal mode True:D
19、ialog box displays OK and As Boolean)Cancel buttons.Function ShowDialog(ByVal template Combines LoadDialogand As String)As ObjectShowDialog:Loads a templatefile,displays the dialog box,andreturns element collection.Property Title As StringTitle of dialog box.Property Width As LongWidth of dialog box
20、 in pixels.Getting access to template elementsDisplaying your HTML template alone wont be enough.You need a way to accessthe elements in your HTML template in order to query their values.After all,youwant to know what the user has entered into your form(see Figure 19-2).Figure 19-2:Examine the eleme
21、nts accessible in your HTML template.542Part V:Accessing Hidden ComponentsII4684-8 ch19.f.qc 3/3/00 9:43 AM Page 542The COM object returns a reference to the element collection.This collectioncontains references to all the elements you placed onto your HTML page.Lets check out how to read this colle
22、ction:19-2.VBSset tool=CreateObject(“Web.dialog”)current pathmyname=WScript.ScriptFullNamecp=left(myname,InstrRev(myname,“”)templatetemplate=cp&“template.htm”size dialogtool.Resizable=truetool.Width=400tool.Height=300tool.Title=“My Form”tool.SetIcon“shell32.dll”,3 display dialog and return object re
23、ferenceset webcontrol=tool.ShowDialog(template)read all named elementsfor each element in webcontrolon error resume nextelname=element.nameif not err.number=0 then element has no name tag!elname=“no name property,type:“&TypeName(element)end ifon error goto 0list=list&elname&vbCrnextMsgBox listThis t
24、ime,once you click OK,the script returns a list of all the elementsinside your HTML template.There are a lot of unnamed control elements,but you also see all the named elements.Named elements are elements you assigned a NAME=tag.Now its easy to retrieve the results someone has entered into your form
25、:19-3.VBSset tool=CreateObject(“Web.dialog”)current pathmyname=WScript.ScriptFullNamecp=left(myname,InstrRev(myname,“”)Chapter 19:Using the WebBrowser Control543II4684-8 ch19.f.qc 3/3/00 9:43 AM Page 543 templatetemplate=cp&“template.htm”size dialogtool.Resizable=truetool.Width=400tool.Height=300too
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Windows 脚本 编程 核心技术 Chapter19
限制150内