一些使用技巧
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 S Webbrowser1_DownloadComplete()
Set M_Dom = Webbrowser1.Document
End S
Rem 好了,右键菜单没有了
2、控件调用和获得收藏夹里面
基本上用 specialfolder(6 ) 就可以得到收藏夹的路径, 然后你可以用dir去循环读入每个目录,然后dir里面的file, file的名字就是你要的收藏的名字, 路径可以自己根据从上面得到的路径去得到.
如果你不用dir也可以用vb的dir控件.
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Plic Type ITEMIDLIST
mkid As SHITEMID
End Type
Plic 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 S Command1_Click()
'请先选中一些内容
Me.WebBrowser1.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
MsgBox Clipboard.GetText
End S
==========================================================================
5、用IE来下载文件
Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long
Private S Command1_Click()
Dim sDownload As String
sDownload = StrConv(Text1.Text, vbUnicode)
Call DoFileDownload(sDownload)
End S
Text1.Text = " http://www.chat.ru/~softdaily/fo-ag162.zip"
Form1.Caption = "Aiograbber 1.62 Full"
Text2.Text = " http://www6.50megs.com/aiograbber/demos/cr-ag161.zip"
End S
6、我要动态加载和删除WebBrowser控件应该怎么做?
Private S Command1_Click()
Form1.Controls.Add "shell.explorer.2", "NewWeb", Form1
With Form1!NewWeb
.Visible = Tr
.Width = 10000
.Height = 10000
.Left = 0
.Top = 0
.Navigate2 " www.csdn.net"
End With
End S
Private S Command2_Click()
Controls.Remove Form1!newweb
End S
Form1.Controls.Add "shell.explorer.2", newweb(newweb.Count), Form1
With Form1!newweb(newweb.Count)
.Visible = Tr
.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 S AddWeb_Click()
For i = 1 To 10
Load NewWeb(i)
NewWeb(i).Top = i * 100
NewWeb(i).Left = i * 100
NewWeb(i).Visible = Tr
Next i
End S
Private S DelWeb_Click()
For i = 1 To 10
Unload NewWeb(i)
Next i
End S
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 S 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 bjStream = .GetStream
Call objStream.SaveToFile("f:/test.mht", adSaveCreateOverWrite)
End With
End S
'首先加入对ADODB和CDO(Microsoft CDO for Windows 2000 Library,对应文件名为CDOSYS.dll)的引用
Private S 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 utstream = message.GetStream
Call Outstream.SaveToFile("c:/test.mht", ADODB.SaveOptionsEnum.adSaveCreateOverWrite)
MsgBox "完成"
Exit S
myerr1:
Set message = Nothing
Set utstream = Nothing
End S
8、在WebBrowser控件调用收藏夹和在收藏夹里添加收藏
Option Explicit
Private S Command1_Click()
Dim ObjSUH As New ShellUIHelper
ObjSUH.AddFavorite " http://www.csdn.net", "CSDN"
Set bjSUH = Nothing
End S
9、如何使网页不出现滚动条:
Private S mnuScroll1_Click()
'注意:必须在网页完全显示之后才可以运行
WebBrowser1.Document.body.Scroll = "no" '不显示滚动条的办法
End S
Private S mnuScroll2_Click()
'注意:必须在网页完全显示之后才可以运行
WebBrowser1.Document.body.Scroll = "Auto" '显示滚动条的办法
End S
==============================================================
10、如何获得网页中被选中部分的HTML:
Private S Command1_Click()
Dim objSelection
Dim objTxtRange
Set bjSelection = WebBrowser1.Document.selection
If Not (objSelection Is Nothing) Then
Set bjTxtRange = objSelection.createRange
If Not (objTxtRange Is Nothing) Then
Debug.Print objTxtRange.htmlText
Set bjTxtRange = Nothing
End If
Set bjSelection = Nothing
End If
End S
Private S Form_Load()
WebBrowser1.Navigate " http://www.applevb.com"
End S
11、页面滚动:
Private S Command2_Click()
WebBrowser1.Document.parentwindow.scrollby 0, 30
End S
Private S Form_Load()
WebBrowser1.Navigate http://www.applevb.com
End S
点击Command2就可以使当前页面向下滚动30像素
12、判断页面是否可以前进后退
Private S Command1_Click()
WebBrowser1.GoForward
End S
Private S Command2_Click()
WebBrowser1.GoBack
End S
Private S Form_Load()
WebBrowser1.Navigate http://www.applevb.com
End S
Private S 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 S
上面的command1是前进按钮,command2是后退按钮
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 S Webbrowser1_DownloadComplete()
Set M_Dom = Webbrowser1.Document
End S
Rem 好了,右键菜单没有了
2、控件调用和获得收藏夹里面
基本上用 specialfolder(6 ) 就可以得到收藏夹的路径, 然后你可以用dir去循环读入每个目录,然后dir里面的file, file的名字就是你要的收藏的名字, 路径可以自己根据从上面得到的路径去得到.
如果你不用dir也可以用vb的dir控件.
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Plic Type ITEMIDLIST
mkid As SHITEMID
End Type
Plic 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 S Command1_Click()
'请先选中一些内容
Me.WebBrowser1.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
MsgBox Clipboard.GetText
End S
==========================================================================
5、用IE来下载文件
Private Declare Function DoFileDownload Lib "shdocvw.dll" (ByVal lpszFile As String) As Long
Private S Command1_Click()
Dim sDownload As String
sDownload = StrConv(Text1.Text, vbUnicode)
Call DoFileDownload(sDownload)
End S
Text1.Text = " http://www.chat.ru/~softdaily/fo-ag162.zip"
Form1.Caption = "Aiograbber 1.62 Full"
Text2.Text = " http://www6.50megs.com/aiograbber/demos/cr-ag161.zip"
End S
6、我要动态加载和删除WebBrowser控件应该怎么做?
Private S Command1_Click()
Form1.Controls.Add "shell.explorer.2", "NewWeb", Form1
With Form1!NewWeb
.Visible = Tr
.Width = 10000
.Height = 10000
.Left = 0
.Top = 0
.Navigate2 " www.csdn.net"
End With
End S
Private S Command2_Click()
Controls.Remove Form1!newweb
End S
Form1.Controls.Add "shell.explorer.2", newweb(newweb.Count), Form1
With Form1!newweb(newweb.Count)
.Visible = Tr
.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 S AddWeb_Click()
For i = 1 To 10
Load NewWeb(i)
NewWeb(i).Top = i * 100
NewWeb(i).Left = i * 100
NewWeb(i).Visible = Tr
Next i
End S
Private S DelWeb_Click()
For i = 1 To 10
Unload NewWeb(i)
Next i
End S
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 S 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 bjStream = .GetStream
Call objStream.SaveToFile("f:/test.mht", adSaveCreateOverWrite)
End With
End S
'首先加入对ADODB和CDO(Microsoft CDO for Windows 2000 Library,对应文件名为CDOSYS.dll)的引用
Private S 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 utstream = message.GetStream
Call Outstream.SaveToFile("c:/test.mht", ADODB.SaveOptionsEnum.adSaveCreateOverWrite)
MsgBox "完成"
Exit S
myerr1:
Set message = Nothing
Set utstream = Nothing
End S
8、在WebBrowser控件调用收藏夹和在收藏夹里添加收藏
Option Explicit
Private S Command1_Click()
Dim ObjSUH As New ShellUIHelper
ObjSUH.AddFavorite " http://www.csdn.net", "CSDN"
Set bjSUH = Nothing
End S
9、如何使网页不出现滚动条:
Private S mnuScroll1_Click()
'注意:必须在网页完全显示之后才可以运行
WebBrowser1.Document.body.Scroll = "no" '不显示滚动条的办法
End S
Private S mnuScroll2_Click()
'注意:必须在网页完全显示之后才可以运行
WebBrowser1.Document.body.Scroll = "Auto" '显示滚动条的办法
End S
==============================================================
10、如何获得网页中被选中部分的HTML:
Private S Command1_Click()
Dim objSelection
Dim objTxtRange
Set bjSelection = WebBrowser1.Document.selection
If Not (objSelection Is Nothing) Then
Set bjTxtRange = objSelection.createRange
If Not (objTxtRange Is Nothing) Then
Debug.Print objTxtRange.htmlText
Set bjTxtRange = Nothing
End If
Set bjSelection = Nothing
End If
End S
Private S Form_Load()
WebBrowser1.Navigate " http://www.applevb.com"
End S
11、页面滚动:
Private S Command2_Click()
WebBrowser1.Document.parentwindow.scrollby 0, 30
End S
Private S Form_Load()
WebBrowser1.Navigate http://www.applevb.com
End S
点击Command2就可以使当前页面向下滚动30像素
12、判断页面是否可以前进后退
Private S Command1_Click()
WebBrowser1.GoForward
End S
Private S Command2_Click()
WebBrowser1.GoBack
End S
Private S Form_Load()
WebBrowser1.Navigate http://www.applevb.com
End S
Private S 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 S
上面的command1是前进按钮,command2是后退按钮