《VB操作网页代码.doc》由会员分享,可在线阅读,更多相关《VB操作网页代码.doc(17页珍藏版)》请在金锄头文库上搜索。
1、1、 防止新窗口里头打开网页代码1:Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)Dim frm As Form1Set frm = New Form1frm.Visible = TrueSet ppDisp = frm.WebBrowser1.objectEnd Sub代码2:有这段代码, 有许多网页会出错,经常提示脚本错误,可以用silent属性为True来屏蔽,不过也有些不足!Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel
2、 As Boolean)Cancel = TrueWebBrowser1.Navigate2 WebBrowser1.Document.activeElement.hrefEnd Sub代码3:Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)On Error Resume NextDim frmWB As Form1Set frmWB = New Form1frmWB.WebBrowser1.RegisterAsBrowser = TrueSet ppDisp = frmWB.WebBrowser1.
3、objectfrmWB.Visible = TruefrmWB.Top = Form1.TopfrmWB.Left = Form1.LeftfrmWB.Width = Form1.WidthfrmWB.Height = Form1.HeightEnd Sub功能差不多,任选一个。2、 去掉滚动条Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)WebBrowser1.Document.Body.Scroll = noEnd Sub实际上上面的效果不咋地,如果懂得HTML知识, 你可以在读
4、取网页的时候,读取HTML源码, 查找替换, 再写入只需在 之间插入代码: 即可。其中x表示水平滚动条,将其改为y的话就可以隐藏垂直滚动条。当然也有其他方法, 比如修改网页的尺寸呀? 有的时候部分元素的居中改为左对齐也能有效果将WebBrower放在PictureBox控件中,用PictureBox的边框挡住WebBrower的边框。例如,将WebBrowser1放大点,将PictureBox变小点.PictureBox的appearance设置为0-flat,呵呵,OK3、 禁止鼠标右键Private Function M_Dom_oncontextmenu() As BooleanWebb
5、rowser1.Document.oncontextmenu = FalseEnd Function引用Microsoft HTML OBject LibraryDim WithEvents M_Dom As MSHTML.HTMLDocumentPrivate Function M_Dom_oncontextmenu() As BooleanM_Dom_oncontextmenu = FalseEnd FunctionPrivate Sub Webbrowser1_DownloadComplete()Set M_Dom = Webbrowser1.DocumentEnd Sub4、 如何获得
6、网页的内容先给个例子:innerHTML:设置或获取位于对象起始和结束标签内的 HTML测试一下:这是一个层function getinnerHTML()alert(document.getElementById(d).innerHTML)function setinnerHTML()document.getElementById(d).innerHTML=这是一个层,嘿嘿5、 多框架框架页面访问下面两句可以访问到多框架内容.Document.ParentWindow.Frames.Length.Document.ParentWindow.Frames(1).Document.all.tags
7、(a)等待多框架网页全部加载完毕, 否则出错 While .Busy Or .ReadyState 4 Or .Document.ParentWindow.Frames.Length = 0 DoEvents Wend6、 获得浏览器信息Private Sub Command1_Click() WebBrowser1.Navigate http:/ End Sub Private Sub Command2_Click() Dim oWindow Dim oNav Set oWindow = WebBrowser1.Document.parentWindow Set oNav = oWindow
8、.navigator Debug.Print oNav.userAgent Set oWindow = Nothing Set oNav = Nothing End Sub7、 弹出Webbrowser消息窗口Dim oWindowSet oWindow = WebBrowser1.Document.parentWindowoWindow.confirm abcdVB调用webbrowser技巧集28、 向Webbrowser中写入HTML内容的几种方法向Webbrowser中写入HTML内容的几种方法首先在Form_Load中加入WebBrowser1.Navigate about:blan
9、k确保Webbrowser1可用方法1:Dim s As StringDim stream As IStreams = s = s + s = s + s = s + hello worlds = s + s = s + WebBrowser1.Document.Write s方法2:Dim oSet o = WebBrowser1.Document.selection.createrangeDebug.Print oIf (Not o Is Nothing) Theno.pasteHTML 哈哈Set o = NothingEnd If方法3:插入文本框Dim oSet o = WebBro
10、wser1.Document.selection.createrangeo.execCommand InsertTextArea, False, xxx9、 控制页面滚动WebBrowser1.Document.parentwindow.scrollby 0, 3010、 判断页面是否可以前进后退Private Sub Command1_Click()WebBrowser1.GoForwardEnd SubPrivate Sub Command2_Click()WebBrowser1.GoBackEnd SubPrivate Sub Form_Load()WebBrowser1.Navigat
11、e http:/End SubPrivate Sub WebBrowser1_CommandStateChange(ByVal Command As Long, ByVal Enable As Boolean)If (Command = CSC_NAVIGATEBACK) ThenCommand2.Enabled = EnableEnd IfIf (Command = CSC_NAVIGATEFORWARD) ThenCommand1.Enabled = EnableEnd IfEnd Sub11、 如何获得网页中被选中部分的HTMLPrivate Sub Command1_Click()Di
12、m objSelectionDim objTxtRangeSet objSelection = WebBrowser1.Document.selectionIf Not (objSelection Is Nothing) ThenSet objTxtRange = objSelection.createRangeIf Not (objTxtRange Is Nothing) ThenDebug.Print objTxtRange.htmlTextSet objTxtRange = NothingEnd IfSet objSelection = NothingEnd IfEnd Sub12、 N
13、avigate的参数调用请问:在WebBrwoser控件里提供的Navigate或者Navigate2方法中提供了传递数据的参数,调用方式为:WebBrowser1.Navigate2(URL,Flags,TargetFrameName,PostData,Headers)其中PostData参数就是一个提交参数字符串,例如name=aaa&password=123,但问题是为什么这个方法并不是有效的,服务器端不能取得数据?如果这个方法是有效的话就不需要用一段html代码模拟这种调用了。下面代码能检测出程序post出去的消息Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)MsgBox PostDataEnd Sub