Visual Basic 2005——如何将Windows Form表单模拟成一个IE浏览器
在撰写「Visual Basic 2005程序开发与界面设计秘诀」一书时突发奇想,是不是可以将Windows Form表单模拟成一个IE浏览器呢?其实在有了WebBrowser控制项之后,这样的想法不难达成。不过问题在于,我们如何取得电脑中的「收藏夹」信息,并将其阶层式内容透过MenuStrip控制项来呈现呢?而且如何让使用者通过我们提供的功能表来整理收藏夹呢?
图表1
图表2
程序范例CH5_DemoForm002.vb是我们的具体实作(如图表1所示),它提供了完整的「收藏夹」资讯,而且也提供具备实际功能的「添加到收藏夹」与「整理收藏夹」功能表项目。程序范例CH5_DemoForm002.vb的功能特性与撰写技巧说明如下:
q 图表2所示者是程序范例CH5_DemoForm002.vb的执行画面,我们发现「收藏夹」主功能表项目之下会将您电脑中的「收藏夹」资讯完整呈现出来,而且当选取某一个功能表项目时,也会立即导览至该所代表的网页。
大家必须了解,您在Microsoft Internet Explorer中所组织出的「收藏夹」资讯其实是以目录结构的形式储存于您的电脑中,所以我们只需将该目录结构转换成「收藏夹」主功能表项目之下的下拉式功能表即可。基本上,我们是利用Environment.SpecialFolder.Favorites来取得「收藏夹」之目录结构的位置,然后透过递回呼叫的方式,将它们转换成功能表。相关程序码列示如下:
Private Sub CH5_DemoForm002_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
…
…
' 取得收藏夹系统资料夹路径。
Dim myFavDir As DirectoryInfo = New _
DirectoryInfo(Environment.GetFolderPath( _
Environment.SpecialFolder.Favorites))
RefreshDir(myFavDir)
End Sub
Private Sub RefreshDir(ByVal myFavDir As DirectoryInfo)
' 清除所有的ToolStripMenuItem。
Me.tsmiFavorite.DropDownItems.Clear()
Me.addToFavoritesToolStripMenuItem.Name = _
"addToFavoritesToolStripMenuItem"
Me.addToFavoritesToolStripMenuItem.Size = _
New System.Drawing.Size(167, 22)
Me.addToFavoritesToolStripMenuItem.Text = _
"添加到收藏夹(&A)..."
' 加入ToolStripMenuItem。
Me.tsmiFavorite.DropDownItems.Add( _
addToFavoritesToolStripMenuItem)
Me.organizeFavoritesToolStripMenuItem.Name = _
"organizeFavoritesToolStripMenuItem"
Me.organizeFavoritesToolStripMenuItem.Size = _
New System.Drawing.Size(167, 22)
Me.organizeFavoritesToolStripMenuItem.Text = _
"整理收藏夹(&O)..."
' 加入ToolStripMenuItem。
Me.tsmiFavorite.DropDownItems.Add( _
organizeFavoritesToolStripMenuItem)
Me.toolStripMenuItem13.Name = "toolStripMenuItem13"
Me.toolStripMenuItem13.Size = _
New System.Drawing.Size(164, 6)
' 加入ToolStripMenuItem。
Me.tsmiFavorite.DropDownItems.Add(toolStripMenuItem13)
Dim myFileSystemInfo As FileSystemInfo() = _
myFavDir.GetFileSystemInfos()
GetDir(myFileSystemInfo, tsmiFavorite)
End Sub
Private Sub GetDir(ByVal myFavDir As FileSystemInfo(), _
ByVal myToolStripMenuItem As ToolStripMenuItem)
Dim myStr, myUrl, myDescription As String
Dim URLPrefix As String = "url="
Dim DescriptionPrefix As String = "description="
If myFavDir Is Nothing Then
Throw New ArgumentNullException("myFavDir")
End If
Dim FileSystemInfo As FileSystemInfo
Dim ItemIndex As Integer = 1
For Each FileSystemInfo In myFavDir
' 判断是否为目录。
If TypeOf FileSystemInfo Is DirectoryInfo Then
Dim FileToolStripMenuItem As ToolStripMenuItem = _
New ToolStripMenuItem( _
FileSystemInfo.ToString, My.Resources.Folder16)
myToolStripMenuItem.DropDownItems.Add( _
FileToolStripMenuItem)
Dim dInfo As DirectoryInfo = _
CType(FileSystemInfo, DirectoryInfo)
' 递回呼叫方法显示所有的子目录内容。
GetDir( _
dInfo.GetFileSystemInfos(), FileToolStripMenuItem)
' 判断是否为档案。
ElseIf TypeOf FileSystemInfo Is FileInfo Then
myUrl = ""
myDescription = ""
Using SR As New StreamReader( _
FileSystemInfo.FullName, Encoding.Default)
myStr = SR.ReadLine
Do Until myStr Is Nothing
If myStr.ToLower.StartsWith(URLPrefix) Then
myUrl = myStr.Substring(URLPrefix.Length)
ElseIf myStr.ToLower.StartsWith(DescriptionPrefix) Then
myDescription = _
myStr.Substring(DescriptionPrefix.Length)
End If
myStr = SR.ReadLine
Loop
End Using
Dim FileToolStripMenuItem As ToolStripMenuItem = _
New ToolStripMenuItem( _
FileSystemInfo.ToString.Substring( _
0, FileSystemInfo.ToString.LastIndexOf(".")), _
My.Resources.link16, New EventHandler( _
AddressOf FileToolStripMenuItem_Click))
' 将Url指派给功能表项目的Tag属性。
FileToolStripMenuItem.Tag = myUrl
' 将Url指派给功能表项目的ToolTipText属性。
FileToolStripMenuItem.ToolTipText = myUrl
myToolStripMenuItem.DropDownItems.Add( _
FileToolStripMenuItem)
End If
ItemIndex += 1
Next FileSystemInfo
End Sub
Sub FileToolStripMenuItem_Click(ByVal sender As Object, _
ByVal e As EventArgs)
Me.webBrowser1.Navigate( _
CType(sender, ToolStripMenuItem).Tag.ToString)
End Sub
q 本程序范例的另外两项重要功能,就是可以实际执行「添加到收藏夹」与「整理收藏夹」两项作业(如图表3与4所示)。相关程序码列示如下:
Private Declare Function DoOrganizeFavDlg Lib "shdocvw" _
(ByVal hWnd As IntPtr, ByVal lpszRootFolder As String) As Int32
' 添加到收藏夹。
Private Sub addToFavoritesToolStripMenuItem_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles addToFavoritesToolStripMenuItem.Click
Dim shl As SHDocVw.ShellUIHelper = _
New SHDocVw.ShellUIHelper()
Dim title As Object = CObj(webBrowser1.DocumentTitle)
shl.AddFavorite(webBrowser1.Url.ToString(), title)
Dim myFavDir As DirectoryInfo = New _
DirectoryInfo(Environment.GetFolderPath( _
Environment.SpecialFolder.Favorites))
RefreshDir(myFavDir)
End Sub
' 整理收藏夹。
Private Sub organizeFavoritesToolStripMenuItem_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles organizeFavoritesToolStripMenuItem.Click
Dim success As Int32
Dim myFavDir As DirectoryInfo = New _
DirectoryInfo(Environment.GetFolderPath( _
Environment.SpecialFolder.Favorites))
success = DoOrganizeFavDlg(Me.Handle, _
Environment.GetFolderPath( _
Environment.SpecialFolder.Favorites))
RefreshDir(myFavDir)
End Sub
图表3
注:图中“我的最爱”即为简体系统中的收藏夹。