WebBrower的应用和功能扩展(二)

本文介绍了一种在自定义浏览器中实现地址栏的方法,通过VB.NET代码实现了地址栏的显示、链接管理和页面跳转等功能,并提供了完整的代码示例。

Author:

 

先看需要的功能,如图红框圈了的。囿于能力,有些不能实现。

 

这里选实现地址栏,效果如图:

 

演示代码:

 

Public   Class  ExplorerForm

    
Private   Sub  ExplorerForm_Load( ByVal  sender  As   Object ByVal  e  As  System.EventArgs)  Handles   Me .Load
        
Me .AddressToolStrip1.AddLinkItem( " 水如烟.村的专栏 " " http://blog.youkuaiyun.com/lzmtw " )
        
Me .AddressToolStrip1.AddLinkItem( " 凤凰网 " " http://www.phoenixtv.com/ " True )
    
End Sub

End Class

 

AddressToolStrip.vb

 

Imports  System.ComponentModel

Namespace  LzmTW.uSystem.uWindows.uForm.Web
    
Public   Class  AddressToolStrip

        
Public   Property  LinkItemsVisible()  As   Boolean
            
Get
                
Return   Me .LinkToolStripLabel.Visible
            
End   Get
            
Set ( ByVal  value  As   Boolean )
                
If   Me .LinkToolStripLabel.Visible  =  value  Then   Return

                
Me .LinkToolStripLabel.Visible  =  value
                
Me .LinkToolStripDropDownButton.Visible  =  value
            
End   Set
        
End Property

#Region  "处理ComboBox的宽度"
        
Private  gOtherLen  As   Integer

        
Private   Sub  RelfeshOtherLen()
            gOtherLen 
=   Me .AddressToolStripLabel.Width  +   Me .GotoToolStripButton.Width  +   16
            
If   Me .LinkToolStripLabel.Visible  Then
                gOtherLen 
+=   Me .LinkToolStripLabel.Width  +   Me .LinkToolStripDropDownButton.Width
            
End   If
        
End Sub

        
Private   Sub  RefleshAddressComboBoxWith()
            
If  gOtherLen  =   0   Then   Return

            
Me .PerformLayout()
            
If   Me .Width  <   Me .gOtherLen  Then
                
Me .AddressToolStripComboBox.AutoSize  =   True
            
Else
                
Me .AddressToolStripComboBox.AutoSize  =   False
                
Me .AddressToolStripComboBox.Width  =   Me .Width  -  gOtherLen
            
End   If
            
Me .ResumeLayout( False )
        
End Sub

        
Private   Sub  AddressToolStrip_ParentChanged( ByVal  sender  As   Object ByVal  e  As  System.EventArgs) _
        
Handles   Me .ParentChanged
            gOtherLen 
=   Me .AddressToolStripLabel.Width _
            
+   Me .GotoToolStripButton.Width _
            
+   Me .LinkToolStripLabel.Width _
            
+   Me .LinkToolStripDropDownButton.Width _
            
+   16

            RefleshAddressComboBoxWith()
        
End Sub

        
Private   Sub  AddressToolStrip_Resize( ByVal  sender  As   Object ByVal  e  As  System.EventArgs)  Handles   Me .Resize
            RefleshAddressComboBoxWith()
        
End Sub

        
Private   Sub  LinkToolStripLabel_VisibleChanged( ByVal  sender  As   Object ByVal  e  As  System.EventArgs) _
        
Handles  LinkToolStripLabel.VisibleChanged
            RelfeshOtherLen()
            RefleshAddressComboBoxWith()
        
End Sub

        
Private   Sub  AddressToolStrip_LayoutCompleted( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs) _
        
Handles   MyBase .LayoutCompleted
            RefleshAddressComboBoxWith()
        
End Sub
#End Region

        
Public   Sub  AddLinkItem( ByVal  name  As   String ByVal  url  As   String Optional   ByVal  AddBeforeSeparator  As   Boolean   =   False )
            
If  AddBeforeSeparator  Then
                
Me .LinkToolStripDropDownButton.DropDownItems.Add( New  ToolStripSeparator)
            
End   If

            
Dim  mItem  As   New  ToolStripMenuItem(name)
            mItem.Tag 
=  url
            
AddHandler  mItem.Click,  AddressOf  OnLinkItemClick
            
Me .LinkToolStripDropDownButton.DropDownItems.Add(mItem)
        
End Sub

        
Private   Sub  OnLinkItemClick( ByVal  sender  As   Object ByVal  e  As  System.EventArgs)
            
Dim  mUri  As   Object   =   CType (sender, ToolStripMenuItem).Tag
            
If  mUri  IsNot   Nothing   Then
                OnUriChanged(sender, mUri.ToString)
            
End   If
        
End Sub

        
Private   Sub  AddressToolStripComboBox_SelectedIndexChanged( ByVal  sender  As   Object ByVal  e  As  System.EventArgs) _
        
Handles  AddressToolStripComboBox.SelectedIndexChanged
            OnUriChanged(sender, 
Me .AddressToolStripComboBox.Text)
        
End Sub

        
Private   Sub  AddressToolStripComboBox_KeyUp( ByVal  sender  As   Object ByVal  e  As  System.Windows.Forms.KeyEventArgs) _
        
Handles  AddressToolStripComboBox.KeyUp
            
If  e.KeyCode  =  Keys.Enter  Then
                OnUriChanged(sender, 
Me .AddressToolStripComboBox.Text)
            
End   If
        
End Sub

        
Private   Sub  GotoToolStripButton_Click( ByVal  sender  As   Object ByVal  e  As  System.EventArgs) _
        
Handles  GotoToolStripButton.Click
            OnUriChanged(sender, 
Me .AddressToolStripComboBox.Text)
        
End Sub

        
Private   Sub  OnUriChanged( ByVal  sender  As   Object ByVal  uri  As   String )
            
Try
                
Dim  mUri  As   New  Uri(uri)
                AddUriToCurrent(mUri)
                
If   Me .gWebBrowser  IsNot   Nothing   Then
                    
Me .gWebBrowser.Navigate(mUri)
                
End   If
            
Catch  ex  As  Exception
            
End   Try
        
End Sub

        
Private  gCurrentUri  As   New  Hashtable

        
Private   Sub  SetUri( ByVal  uri  As  Uri)
            
Me .AddressToolStripComboBox.Text  =  uri.AbsoluteUri
        
End Sub

        
Private   Sub  AddUriToCurrent( ByVal  uri  As  Uri)
            
If   Not  gCurrentUri.ContainsKey(uri.AbsoluteUri)  Then
                gCurrentUri.Add(uri.AbsoluteUri, uri)
                
Me .AddressToolStripComboBox.Items.Add(uri.AbsoluteUri)
            
End   If
        
End Sub

        
Private  gWebBrowser  As  System.Windows.Forms.WebBrowser

        
< DesignerSerializationVisibility(DesignerSerializationVisibility.Visible) >  _
        
Public   Property  WebBrowser()  As  System.Windows.Forms.WebBrowser
            
Get
                
Return   Me .gWebBrowser
            
End   Get
            
Set ( ByVal  value  As  System.Windows.Forms.WebBrowser)
                
If  gWebBrowser  IsNot   Nothing   Then
                    
RemoveHandler  gWebBrowser.Navigated,  AddressOf  WebBrowser_Navigated
                    gWebBrowser 
=   Nothing
                
End   If

                
If  value  IsNot   Nothing   Then
                    gWebBrowser 
=  value
                    
AddHandler  gWebBrowser.Navigated,  AddressOf  WebBrowser_Navigated
                
End   If
            
End   Set
        
End Property

        
Private   Sub  WebBrowser_Navigated( ByVal  sender  As   Object ByVal  e  As  System.Windows.Forms.WebBrowserNavigatedEventArgs)

            
Me .SetUri(gWebBrowser.Url)
            
Me .AddUriToCurrent(gWebBrowser.Url)
        
End Sub
    
End Class
End Namespace

 

Namespace  LzmTW.uSystem.uWindows.uForm
    
Partial   Class  AddressToolStrip
        
Inherits  System.Windows.Forms.ToolStrip

        
' <System.Diagnostics.DebuggerNonUserCode()> _
         Public   Sub   New ( ByVal  container  As  System.ComponentModel.IContainer)
            
MyClass .New()

            
' Windows.Forms 类撰写设计器支持所必需的
             If  (container  IsNot   Nothing Then
                container.Add(
Me )
            
End   If
        
End Sub

        
< System.Diagnostics.DebuggerNonUserCode() >  _
        
Public   Sub   New ()
            
MyBase .New()

            
' 组件设计器需要此调用。
            InitializeComponent()

        
End Sub

        
' Component 重写 Dispose,以清理组件列表。
         < System.Diagnostics.DebuggerNonUserCode() >  _
        
Protected   Overrides   Sub  Dispose( ByVal  disposing  As   Boolean )
            
Try
                
If  disposing  AndAlso  components  IsNot   Nothing   Then
                    components.Dispose()
                
End   If
            
Finally
                
MyBase .Dispose(disposing)
            
End   Try
        
End Sub

        
' 组件设计器所必需的
         Private  components  As  System.ComponentModel.IContainer

        
' 注意: 以下过程是组件设计器所必需的
         ' 可使用组件设计器修改它。
         ' 不要使用代码编辑器修改它。
         < System.Diagnostics.DebuggerStepThrough() >  _
    
Private   Sub  InitializeComponent()
            
Dim  resources  As  System.ComponentModel.ComponentResourceManager  =   New  System.ComponentModel.ComponentResourceManager( GetType (AddressToolStrip))
            
Me .AddressToolStripLabel  =   New  System.Windows.Forms.ToolStripLabel
            
Me .AddressToolStripComboBox  =   New  System.Windows.Forms.ToolStripComboBox
            
Me .AddressToolStripSeparator  =   New  System.Windows.Forms.ToolStripSeparator
            
Me .GotoToolStripButton  =   New  System.Windows.Forms.ToolStripButton
            
Me .LinkToolStripLabel  =   New  System.Windows.Forms.ToolStripLabel
            
Me .LinkToolStripDropDownButton  =   New  System.Windows.Forms.ToolStripDropDownButton
            
Me .SuspendLayout()
            
'
             ' AddressToolStripLabel
             '
             Me .AddressToolStripLabel.Name  =   " AddressToolStripLabel "
            
Me .AddressToolStripLabel.Size  =   New  System.Drawing.Size( 47 22 )
            
Me .AddressToolStripLabel.Text  =   " 地址(&D) "
            
'
             ' AddressToolStripComboBox
             '
             Me .AddressToolStripComboBox.AutoCompleteMode  =  System.Windows.Forms.AutoCompleteMode.SuggestAppend
            
Me .AddressToolStripComboBox.AutoCompleteSource  =  System.Windows.Forms.AutoCompleteSource.AllSystemSources
            
Me .AddressToolStripComboBox.AutoSize  =   False
            
Me .AddressToolStripComboBox.Name  =   " AddressToolStripComboBox "
            
Me .AddressToolStripComboBox.Size  =   New  System.Drawing.Size( 121 20 )
            
'
             ' AddressToolStripSeparator
             '
             Me .AddressToolStripSeparator.Alignment  =  System.Windows.Forms.ToolStripItemAlignment.Right
            
Me .AddressToolStripSeparator.Name  =   " AddressToolStripSeparator "
            
Me .AddressToolStripSeparator.Size  =   New  System.Drawing.Size( 6 6 )
            
'
             ' GotoToolStripButton
             '
             Me .GotoToolStripButton.Alignment  =  System.Windows.Forms.ToolStripItemAlignment.Right
            
Me .GotoToolStripButton.Image  =   CType (resources.GetObject( " GotoToolStripButton.Image " ), System.Drawing.Image)
            
Me .GotoToolStripButton.ImageTransparentColor  =  System.Drawing.Color.Magenta
            
Me .GotoToolStripButton.Name  =   " GotoToolStripButton "
            
Me .GotoToolStripButton.Size  =   New  System.Drawing.Size( 49 20 )
            
Me .GotoToolStripButton.Text  =   " 转到 "
            
'
             ' LinkToolStripLabel
             '
             Me .LinkToolStripLabel.Alignment  =  System.Windows.Forms.ToolStripItemAlignment.Right
            
Me .LinkToolStripLabel.Name  =   " LinkToolStripLabel "
            
Me .LinkToolStripLabel.Size  =   New  System.Drawing.Size( 29 12 )
            
Me .LinkToolStripLabel.Text  =   " 链接 "
            
'
             ' LinkToolStripDropDownButton
             '
             Me .LinkToolStripDropDownButton.Alignment  =  System.Windows.Forms.ToolStripItemAlignment.Right
            
Me .LinkToolStripDropDownButton.AutoToolTip  =   False
            
Me .LinkToolStripDropDownButton.DisplayStyle  =  System.Windows.Forms.ToolStripItemDisplayStyle.Text
            
Me .LinkToolStripDropDownButton.ImageTransparentColor  =  System.Drawing.Color.Magenta
            
Me .LinkToolStripDropDownButton.Name  =   " LinkToolStripDropDownButton "
            
Me .LinkToolStripDropDownButton.Size  =   New  System.Drawing.Size( 30 16 )
            
Me .LinkToolStripDropDownButton.Text  =   " >> "
            
'
             ' AddressToolStrip
             '
             Me .Items.AddRange( New  System.Windows.Forms.ToolStripItem() { Me .AddressToolStripLabel,  Me .AddressToolStripComboBox,  Me .LinkToolStripDropDownButton,  Me .LinkToolStripLabel,  Me .GotoToolStripButton,  Me .AddressToolStripSeparator})
            
Me .ResumeLayout( False )

        
End Sub

        
Private   WithEvents  AddressToolStripLabel  As  System.Windows.Forms.ToolStripLabel
        
Private   WithEvents  AddressToolStripComboBox  As  System.Windows.Forms.ToolStripComboBox
        
Private   WithEvents  AddressToolStripSeparator  As  System.Windows.Forms.ToolStripSeparator
        
Private   WithEvents  GotoToolStripButton  As  System.Windows.Forms.ToolStripButton
        
Private   WithEvents  LinkToolStripLabel  As  System.Windows.Forms.ToolStripLabel
        
Private   WithEvents  LinkToolStripDropDownButton  As  System.Windows.Forms.ToolStripDropDownButton


    
End Class
End Namespace
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值