vb+Webbrowser控件详解2007-12-2218:221、获得浏览器信息:Private Sub Command1_Click()WebBrowser1.Navigate""End SubPrivate Sub Command2_Click()Dim oWindowDim oNavSet oWindow=WebBrowser1.Document.parentWindowSet oNav=oWindow.navigatorDebug.Print erAgentSet oWindow=NothingSet oNav=NothingEnd Sub点击Command1浏览网页,点击Command2在立即窗口中输出浏览器信息。
2、弹出Webbrowser消息窗口Dim oWindowSet oWindow=WebBrowser1.Document.parentWindowoWindow.confirm"abcd"VB调用webbrowser技巧集2向Webbrowser中写入HTML内容的几种方法首先在Form_Load中加入WebBrowser1.Navigate"about:blank"确保Webbrowser1可用方法1:Dim s As StringDim stream As IStreams=""s=s+""s=s+""s=s+"hello world"s=s+""s=s+"WebBrowser1.Document.Write s方法2:Dim oSet o=WebBrowser1.Document.selection.createrange Debug.Print oIf(Not o Is Nothing)Theno.pasteHTML"哈哈"Set o=NothingEnd If方法3:'插入文本框Dim oSet o=WebBrowser1.Document.selection.createrangeo.execCommand"InsertTextArea",False,"xxx"vb调用Webbrowser技巧集31、页面滚动:Private Sub Command2_Click()WebBrowser1.Document.parentwindow.scrollby0,30 End SubPrivate Sub Form_Load()WebBrowser1.Navigate""End Sub点击Command2就可以使当前页面向下滚动30像素2、判断页面是否可以前进后退Private Sub Command1_Click()WebBrowser1.GoForwardEnd SubPrivate Sub Command2_Click()WebBrowser1.GoBackEnd SubPrivate Sub Form_Load()WebBrowser1.Navigate""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 SubVB调用webbrowser技巧集41、如何使网页不出现滚动条:Private Sub mnuScroll1_Click()@#注意:必须在网页完全显示之后才可以运行WebBrowser1.Document.body.Scroll="no"@#不显示滚动条的办法End SubPrivate Sub mnuScroll2_Click()@#注意:必须在网页完全显示之后才可以运行WebBrowser1.Document.body.Scroll="Auto"@#显示滚动条的办法End Sub2、如何获得网页中被选中部分的HTML:Private Sub Command1_Click()Dim 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 SubPrivate Sub Form_Load()WebBrowser1.Navigate""End SubNavigate方法的语法格式为:WebBrowser控件名.Navigate URL[Flags,][TargetFrameName,][PostData,][Headers]----WebBrowser控件支持的主要方法有:GoBack———回退到上一屏。
GoForward———进入到下一屏。
GoHome———回家。
即回到主页。
Stop———停止导航。
Refresh———刷新。
Navigate———导航。
----WebBrowser控件所响应的事件:----BeforeNavigate事件———在开始导航前发生。
一般在此获取完整的URL字符串。
----WebBrowser控件最主要的参数:----URL———获得导航用的标准URL字符串。
例如:它能将””自动翻译为””.URL是Uniform Resource Locator的缩写,是在Internet的WWW服务程序上用于指定信息位置的表示方法。
使用WebBrowser的Navigator或者Navigator2方法打开一个asp文档并且传递参数进去,但是asp文档无法获得参数,请教是什么原因?vb源码:Private Sub cmdSubmit_Click()Dim strURL As String,strFormData As StringDim strData As StringstrURL=Trim$(txtURL.Text)strFormData="name="&Trim$(txtName.Text)&"&password="&Trim$(txtPassword.Text)Call WBTest.Navigate2(strURL,64,"_blank",strFormData,"hello")End Subasp源码:<%@Language=VBScript%><%dim strNamedim strPasswordstrName=Trim(Request.Form("name"))strPassword=Trim(Request.Form("password"))Response.Write(strName)Response.Write(strPassword)if strName="KingZhang"and strPassword="123456"thenResponse.Write("登陆成功")elseResponse.Write("非法登陆用户!")end if%>********************************************************************* Option ExplicitPrivate Sub Command1_Click()Dim szValue As StringWebBrowser1.Document.body.innerHTML="method=post action=http://地址/xxx.php>"WebBrowser1.Document.Forms("post").submitEnd SubPrivate Sub Form_Load()WebBrowser1.Navigate2"about:blank"End SubTop********************************************************************请问:在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 SubWebBrowser的8个方法和13个属性WebBrowser的8个方法和13个属性,以及它们的功能:方法说明GoBack相当于IE的“后退”按钮,使你在当前历史列表中后退一项GoForward相当于IE的“前进”按钮,使你在当前历史列表中前进一项GoHome相当于IE的“主页”按钮,连接用户默认的主页GoSearch相当于IE的“搜索”按钮,连接用户默认的搜索页面Navigate连接到指定的URLRefresh刷新当前页面Refresh2同上,只是可以指定刷新级别,所指定的刷新级别的值来自RefreshConstants枚举表,该表定义在ExDisp.h中,可以指定的不同值如下:REFRESH_NORMAL执行简单的刷新,不将HTTP pragma:no-cache头发送给服务器REFRESH_IFEXPIRED只有在网页过期后才进行简单的刷新REFRESH_CONTINUE仅作内部使用。