WebBrowser控件使用详解

1.禁止右键

看到很多关于WebBrowser控件禁止右键的提问,回复的方法很多,其中有提到使用微软提供的Webbrowser扩展COM服务器对象(WBCustomizer.dll),但是该方法在我们想使用Webbrowser编辑网页(Webbrowser1.Document.execCommand “editMode”)的时候有很多弊端,比如不能显示选中的文本等。另有些方法也就不用一一列举了。
这儿我想提到的是关于MSHTML.HTMLDocument
引用Microsoft HTML OBject Library

Rem #窗体代码#
Dim WithEvents M_Dom As MSHTML.HTMLDocument
Private Function M_Dom_oncontextmenu() As Boolean
         M_Dom_oncontextmenu = False
End Function
Private Sub Webbrowser1_DownloadComplete()
       Set M_Dom = Webbrowser1.Document
End Sub
Rem

好了,右键菜单没有了

2、控件调用和获得收藏夹里面
基本上用 specialfolder(6 ) 就可以得到收藏夹的路径, 然后你可以用dir去循环读入每个目录,然后dir里面的file, file的名字就是你要的收藏的名字, 路径可以自己根据从上面得到的路径去得到.
如果你不用dir也可以用vb的dir控件.

Private Type SHITEMID
     cb As Long
     abID As Byte
End Type
Public Type ITEMIDLIST
     mkid As SHITEMID
End Type
Public Function SpecialFolder(ByRef CSIDL As Long) As String
     ‘locate the favorites folder
     Dim R As Long
     Dim sPath As String
     Dim IDL As ITEMIDLIST
     Const NOERROR = 0
     Const MAX_LENGTH = 260
     R = SHGetSpecialFolderLocation(MDIMain.hwnd, CSIDL, IDL)
     If R = NOERROR Then
         sPath = Space$(MAX_LENGTH)
         R = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath)
         If R Then
             SpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) – 1)
         End If
     End If
End Function

3、全屏

是的,webbrowser本生是一个控件, 你要它全屏,就是要它所在的窗体全屏, 可以用setwindowlong取消窗体的 title, 用Call ShowWindow(FindWindow(“Shell_traywnd”, “”), 0) 隐藏tray,就是下边那个包含开始那一行. 用Call ShowWindow(FindWindow(“Shell_traywnd”, “”), 9) 恢复. 够详细了吧.
然后在form1.windowstate = 2 就可以了.

4、选择网页上的内容。

Private Sub Command1_Click()
‘请先选中一些内容
Me.WebBrowser1.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
MsgBox Clipboard.GetText
End Sub

5、用IE来下载文件

Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long

Private Sub Command1_Click()

    Dim sDownload As String

    sDownload = StrConv(Text1.Text, vbUnicode)
    Call DoFileDownload(sDownload)

End Sub
Private Sub Form_Load()
Text1.Text = "http://www.chat.ru/~softdaily/fo-ag162.zip"
Form1.Caption = "Audiograbber 1.62 Full"
Text2.Text = "http://www6.50megs.com/audiograbber/demos/cr-ag161.zip"
End Sub

6、我要动态加载和删除WebBrowser控件应该怎么做?

Private Sub Command1_Click()
    Form1.Controls.Add "shell.explorer.2", "NewWeb", Form1
     With Form1!NewWeb
         .Visible = True
         .Width = 10000
         .Height = 10000
         .Left = 0
         .Top = 0
         .Navigate2 "www.csdn.net"
     End With
End Sub
Private Sub Command2_Click()
      Controls.Remove Form1!newweb
End Sub
Form1.Controls.Add "shell.explorer.2", newweb(newweb.Count), Form1
     With Form1!newweb(newweb.Count)
         .Visible = True
         .Width = 1000
         .Height = 1000
         .Left = newweb(newweb.Count – 1).Left + newweb(newweb.Count – 1).Width
         .Top = 0
         ‘.Navigate2 "www.csdn.net"
     End With

为什么他说我
Form1.Controls.Add “shell.explorer.2”, newweb(newweb.Count), Form1
这一行错误13 类型不匹配?
ps:我在form中已经有了一个newweb(0)控件

先为一个WebBrowser

Dim i As Integer
Private Sub AddWeb_Click()
     For i = 1 To 10
         Load NewWeb(i)
         NewWeb(i).Top = i * 100
         NewWeb(i).Left = i * 100
         NewWeb(i).Visible = True
     Next i
End Sub
Private Sub DelWeb_Click()
     For i = 1 To 10
         Unload NewWeb(i)
     Next i
End Sub

7、一个把页面保存为MHT(即MHTML)文件

将 pcwak(书剑狂生[MS MVP]) 大侠提供的资料贴出来,以供大家参考:
我终于找到一个把页面保存为MHT(即MHTML)文件的方法了!
首先,在工程中必须要引用一个库:

Library CDO
D:\WINNT\System32\cdosys.dll
Microsoft CDO for Windows 2000 Library

其次,需要Stream对应的接口的支持,如果你一时找不到就使用支持这个的较新的ADO就行了,如

Library ADODB
D:\Program Files\Common Files\system\ado\msado15.dll
Microsoft ActiveX Data Objects 2.5 Library

代码如下,十分简单(同时由于流的特点,显示在实际应用中没必要象本例中那样把文件保存到磁盘上就可直接转换为字符串或字节数组什么的处理的。
另,对于Microsoft CDO for Windows 2000 Library这个库,在WIN98中要怎么使用还没试过,感兴趣的朋友可以试试并跟帖,以丰富完善其内容

Private Sub Command1_Click()
‘ Reference to Microsoft ActiveX Data Objects 2.5 Library
‘ Reference to Microsoft CDO for Windows 2000 Library
Dim iMsg As New CDO.Message
Dim iConf As New CDO.Configuration
Dim objStream As ADODB.Stream
With iMsg
.CreateMHTMLBody "http://www.163.com/";, , _
"domain\username", _
"password"
Set objStream = .GetStream
Call objStream.SaveToFile("f:\test.mht", adSaveCreateOverWrite)
End With
End Sub

2、‘首先加入对ADODB和CDO(Microsoft CDO for Windows 2000 Library,对应文件名为CDOSYS.dll)的引用

Private Sub Command1_Click()
     Dim message As New CDO.message
     Dim Outstream As ADODB.Stream
     On Error GoTo myerr1
     Call message.CreateMHTMLBody("http://www.csdn.net", CDO.CdoMHTMLFlags.cdoSuppressNone, "", "")
     Set Outstream = message.GetStream
     Call Outstream.SaveToFile("c:\test.mht", ADODB.SaveOptionsEnum.adSaveCreateOverWrite)
     MsgBox "完成"

     Exit Sub
myerr1:
     Set message = Nothing
     Set Outstream = Nothing
End Sub

8、在WebBrowser控件调用收藏夹和在收藏夹里添加收藏

Option Explicit
Private Sub Command1_Click()
     Dim ObjSUH As New ShellUIHelper
     ObjSUH.AddFavorite "http://www.csdn.net", "CSDN"
     Set ObjSUH = Nothing
End Sub

9、如何使网页不出现滚动条:

Private Sub mnuScroll1_Click()
‘注意:必须在网页完全显示之后才可以运行
     WebBrowser1.Document.body.Scroll = "no" ‘不显示滚动条的办法
End Sub
Private Sub mnuScroll2_Click()
‘注意:必须在网页完全显示之后才可以运行
     WebBrowser1.Document.body.Scroll = "Auto" ‘显示滚动条的办法
End Sub

10、如何获得网页中被选中部分的HTML:

Private Sub Command1_Click()
     Dim objSelection
     Dim objTxtRange

     Set objSelection = WebBrowser1.Document.selection
     If Not (objSelection Is Nothing) Then
         Set objTxtRange = objSelection.createRange
         If Not (objTxtRange Is Nothing) Then
             Debug.Print objTxtRange.htmlText

             Set objTxtRange = Nothing
         End If
         Set objSelection = Nothing
     End If
End Sub
Private Sub Form_Load()
     WebBrowser1.Navigate "http://www.applevb.com"
End Sub

11、页面滚动:

Private Sub Command2_Click()
     WebBrowser1.Document.parentwindow.scrollby 0, 30
End Sub
Private Sub Form_Load()
      WebBrowser1.Navigate "http://www.applevb.com"
End Sub

点击Command2就可以使当前页面向下滚动30像素

12、判断页面是否可以前进后退

Private Sub Command1_Click()
     WebBrowser1.GoForward
End Sub

Private Sub Command2_Click()
     WebBrowser1.GoBack
End Sub

Private Sub Form_Load()
     WebBrowser1.Navigate "http://www.applevb.com"
End Sub

Private Sub WebBrowser1_CommandStateChange(ByVal Command As Long, ByVal Enable As Boolean)
     If (Command = CSC_NAVIGATEBACK) Then
         Command2.Enabled = Enable
     End If
     If (Command = CSC_NAVIGATEFORWARD) Then
         Command1.Enabled = Enable
     End If
End Sub