DetailPage-MSS-KB

知識庫

文章編號: 311284 - 上次校閱: 2007年5月13日 - 版次: 3.1

 

在此頁中

結論

本文將告訴您,如何在 Visual Basic.NET 中攔截 WebBrowser 控制項的文件事件。

需求

下列清單列出建議的硬體、 軟體、 網路基礎結構及您需要的服務套件:
  • Microsoft Visual Studio.NET
  • Microsoft 網際網路總管 5.5 Service Pack 2 (SP2) 或更新版本
本文假設您已熟悉下列主題:
  • Visual Studio.NET
  • Internet Explorer
  • WebBrowser 控制項

該技巧的描述

WebBrowser 控制項是在 Visual Studio.NET 或較早版本的 Microsoft Visual Studio 中使用簡單控制項。但是,您可能會發現它更難處理 WebBrowser 控制項在 Visual Studio.NET 中的事件。

下列的事件介面公開大部分的應用程式中處理的文件事件:
Mshtml.HTMLDocumentEvents2_Event
				
若要處理事件,您必須建立您自己的 Sub 程序或事件發生時,您可以呼叫的函式。您必須符合所引發事件的簽章。下列的 Sub 程序處理文件的 MouseOver 事件時,例如:
Private Sub document_onmouseover(ByVal e As mshtml.IHTMLEventObj)
				
事件處理常式是就地之後必須攔截該事件。您可以在 WebBrowser 控制項引發攔截事件 DocumentComplete 事件之後的任何時間。以下是勾點語法:
AddHandler CType(document, _
mshtml.HTMLDocumentEvents2_Event).onmouseover, _
AddressOf Me.document_onmouseover
				
這個程式碼會呼叫 AddHandler 陳述式,並使用 CType 函式傳遞事件。CType 函式轉換為適當的型別 (mshtml.HTMLDocumentEvents2_Event) 文件物件,並再從所傳遞 OnMouseOver 事件。Me.document_onmouseoverSub 程序會傳遞至第二個參數 AddressOf 陳述式提供您處理常式的位址。

建立專案並加入程式碼

下列範例中 WebBrowser 控制項瀏覽 http://www.microsoft.com (http://www.microsoft.com) 。載入頁面之後此範例會攔截 OnMouseOverOnClick 事件。範例再加入文字至清單方塊時引發事件。
  1. 啟動 Visual Studio.NET。
  2. 在 Visual Basic.NET 中建立新的 Windows 應用程式專案
  3. 在專案中加入 Microsoft.mshtml 的參考。
  4. 在工具箱按一下 [一般]。
  5. 開啟面板上按一下滑鼠右鍵,然後按一下 [自訂工具箱]。
  6. 選取 [Microsoft Web 瀏覽器] 核取方塊,然後按一下 [確定]
  7. 在工具箱按兩下 [檔案總管]。
  8. 在工具箱中按一下 Windows Form,然後再連按兩下 清單方塊 控制項。
  9. 使它們容易檢視表單上排列控制項。
  10. 將下列程式碼加入至 AssemblyInfo.vb 頂端,取代現有的匯入句子
    Imports System
    Imports System.Drawing
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Windows.Forms
    Imports System.Data
    Imports System.Reflection
    Imports System.Runtime.InteropServices
    Imports System.Threading
    Imports System.Diagnostics
    
  11. Add the following code between Windows Form Designer generated code and End Class in the code view of Form1.vb Public
    Shared dwCookie2 As Integer
        Public Sub add_list(ByVal a As Object)
            ListBox1.Items.Insert(0, a)
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AxWebBrowser1.Navigate2("http://www.microsoft.com")
            Dim x As IEEvents = New IEEvents(AxWebBrowser1)
            x.fm = Me
        End Sub
    
        Private Sub AxWebBrowser1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AxWebBrowser1.Enter
    
        End Sub
    
        Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
            Dim doc As mshtml.HTMLDocument
            doc = AxWebBrowser1.Document
    
            AddHandler CType(doc, _
                mshtml.HTMLDocumentEvents2_Event).onclick, AddressOf Document_onclick
            AddHandler CType(doc, _
         mshtml.HTMLDocumentEvents2_Event).onmouseover, AddressOf Document_onmouseover
        End Sub
    
        Private Sub AxWebBrowser1_BeforeNavigate2(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event) Handles AxWebBrowser1.BeforeNavigate2
            Dim doc As mshtml.HTMLDocument
            doc = AxWebBrowser1.Document
    
            RemoveHandler CType(doc, _
                mshtml.HTMLDocumentEvents2_Event).onclick, _
                AddressOf Document_onclick
            RemoveHandler CType(doc, _
                mshtml.HTMLDocumentEvents2_Event).onmouseover, _
                AddressOf Document_onmouseover
        End Sub
        Private Sub Document_onmouseover(ByVal e As mshtml.IHTMLEventObj)
            ListBox1.Items.Insert(0, "onMouseOver: " & _
                e.srcElement.tagName.ToString())
        End Sub
        Private Function Document_onclick(ByVal e As mshtml.IHTMLEventObj) _
                As Boolean
            ListBox1.Items.Insert(0, "onClick: " & _
                e.srcElement.tagName.ToString())
            Return True
        End Function
    
  12. Add the following code after End Class
    Public Class IEHTMLDocumentEvents
        Implements mshtml.HTMLDocumentEvents2
        Public Sub onactivate(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onactivate
    
        End Sub
    
        Public Sub onafterupdate(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onafterupdate
    
        End Sub
    
        Public Function onbeforeactivate(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onbeforeactivate
            Return True
        End Function
    
        Public Function onbeforedeactivate(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onbeforedeactivate
            Return True
        End Function
    
        Public Sub onbeforeeditfocus(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onbeforeeditfocus
    
        End Sub
    
        Public Function onbeforeupdate(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onbeforeupdate
            Return True
        End Function
    
        Public Sub oncellchange(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.oncellchange
    
        End Sub
    
        Public Function onclick(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onclick
            Return True
        End Function
    
        Public Function oncontextmenu(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.oncontextmenu
            Return True
        End Function
    
        Public Function oncontrolselect(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.oncontrolselect
            Return True
        End Function
    
        Public Sub ondataavailable(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.ondataavailable
    
        End Sub
    
        Public Sub ondatasetchanged(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.ondatasetchanged
    
        End Sub
    
        Public Sub ondatasetcomplete(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.ondatasetcomplete
    
        End Sub
    
        Public Function ondblclick(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.ondblclick
            Return True
        End Function
    
        Public Sub ondeactivate(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.ondeactivate
    
        End Sub
    
        Public Function ondragstart(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.ondragstart
            Return True
        End Function
    
        Public Function onerrorupdate(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onerrorupdate
            Return True
        End Function
    
        Public Sub onfocusin(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onfocusin
    
        End Sub
    
        Public Sub onfocusout(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onfocusout
    
        End Sub
    
        Public Function onhelp(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onhelp
            Return True
        End Function
    
        Public Sub onkeydown(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onkeydown
    
        End Sub
    
        Public Function onkeypress(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onkeypress
            Return True
        End Function
    
        Public Sub onkeyup(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onkeyup
    
        End Sub
    
        Public Sub onmousedown(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmousedown
    
        End Sub
    
        Public Sub onmousemove(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmousemove
    
        End Sub
    
        Public Sub onmouseout(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmouseout
    
        End Sub
    
        Public Sub onmouseover(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmouseover
    
        End Sub
    
        Public Sub onmouseup(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onmouseup
    
        End Sub
    
        Public Function onmousewheel(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onmousewheel
            Return True
        End Function
    
        Public Sub onpropertychange(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onpropertychange
    
        End Sub
    
        Public Sub onreadystatechange(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onreadystatechange
    
        End Sub
    
        Public Sub onrowenter(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onrowenter
    
        End Sub
    
        Public Function onrowexit(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onrowexit
            Return True
        End Function
    
        Public Sub onrowsdelete(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onrowsdelete
    
        End Sub
    
        Public Sub onrowsinserted(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onrowsinserted
    
        End Sub
    
        Public Sub onselectionchange(ByVal pEvtObj As mshtml.IHTMLEventObj) Implements mshtml.HTMLDocumentEvents2.onselectionchange
    
        End Sub
    
        Public Function onselectstart(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onselectstart
            Return True
        End Function
    
        Public Function onstop(ByVal pEvtObj As mshtml.IHTMLEventObj) As Boolean Implements mshtml.HTMLDocumentEvents2.onstop
            Return True
        End Function
    End Class
    
    Public Class IEEvents
        Implements SHDocVw.DWebBrowserEvents2
        Public fm As Form1
        Private icp As System.Runtime.InteropServices.UCOMIConnectionPoint
        Private cookie As Integer = -1
        Private m_ie As AxSHDocVw.AxWebBrowser
    
        Public Sub New(ByRef ie As AxSHDocVw.AxWebBrowser)
            ' Call QueryInterface for IConnectionPointContainer
            m_ie = ie
            Dim icpc As System.Runtime.InteropServices.UCOMIConnectionPointContainer = CType(ie.GetOcx(), System.Runtime.InteropServices.UCOMIConnectionPointContainer)
            ' Find the connection point for the
            ' DWebBrowserEvents2 source interface
            Dim g As Guid = GetType(SHDocVw.DWebBrowserEvents2).GUID
            icpc.FindConnectionPoint(g, icp)
    
            'Pass a pointer to the host to the connection point
            icp.Advise(Me, cookie)
    
            ' Show the browser
            ie.Visible = True
            Dim oURL As Object = "http://www.microsoft.com"
            Dim oEmpty As Object = ""
            ie.Navigate2(oURL, oEmpty, oEmpty, oEmpty, oEmpty)
    
        End Sub
    
    
        Public Sub BeforeNavigate2(ByVal pDisp As Object, ByRef URL As Object, ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData As Object, ByRef Headers As Object, ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.BeforeNavigate2
    
        End Sub
    
        Public Sub ClientToHostWindow(ByRef CX As Integer, ByRef CY As Integer) Implements SHDocVw.DWebBrowserEvents2.ClientToHostWindow
    
        End Sub
    
        Public Sub CommandStateChange(ByVal Command As Integer, ByVal Enable As Boolean) Implements SHDocVw.DWebBrowserEvents2.CommandStateChange
    
        End Sub
    
        Public Sub DocumentComplete(ByVal pDisp As Object, ByRef URL As Object) Implements SHDocVw.DWebBrowserEvents2.DocumentComplete
            Dim doc As mshtml.HTMLDocument = CType(CType(pDisp, SHDocVw.IWebBrowser2).Document, mshtml.HTMLDocument)
            Dim pConPtCon As System.Runtime.InteropServices.UCOMIConnectionPointContainer = CType(doc, System.Runtime.InteropServices.UCOMIConnectionPointContainer)
            Dim guid As Guid = System.Type.GetType("mshtml.HTMLDocumentEvents2").GUID
            Dim pConPt As System.Runtime.InteropServices.UCOMIConnectionPoint
            pConPtCon.FindConnectionPoint(guid, pConPt)
            Dim d As IEHTMLDocumentEvents = New IEHTMLDocumentEvents
            pConPt.Advise(d, Form1.dwCookie2)
    
            Dim iEvent As mshtml.HTMLDocumentEvents2_Event
            iEvent = CType(doc, mshtml.HTMLDocumentEvents2_Event)
            AddHandler iEvent.onclick, AddressOf ClickEventHandler
            AddHandler iEvent.onmouseover, AddressOf MouseOverEventHandler
        End Sub
        Private Function ClickEventHandler(ByVal e As mshtml.IHTMLEventObj) As Boolean
            fm.add_list(e.type + ":" + e.srcElement.tagName)
            Return True
        End Function
        Private Sub MouseOverEventHandler(ByVal e As mshtml.IHTMLEventObj)
            fm.add_list(e.type + ":" + e.srcElement.tagName)
        End Sub
        Public Sub DownloadBegin() Implements SHDocVw.DWebBrowserEvents2.DownloadBegin
    
        End Sub
    
        Public Sub DownloadComplete() Implements SHDocVw.DWebBrowserEvents2.DownloadComplete
    
        End Sub
    
        Public Sub FileDownload(ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.FileDownload
    
        End Sub
    
        Public Sub NavigateComplete2(ByVal pDisp As Object, ByRef URL As Object) Implements SHDocVw.DWebBrowserEvents2.NavigateComplete2
    
        End Sub
    
        Public Sub NavigateError(ByVal pDisp As Object, ByRef URL As Object, ByRef Frame As Object, ByRef StatusCode As Object, ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.NavigateError
    
        End Sub
    
        Public Sub NewWindow2(ByRef ppDisp As Object, ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.NewWindow2
    
        End Sub
    
        Public Sub OnFullScreen(ByVal FullScreen As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnFullScreen
    
        End Sub
    
        Public Sub OnMenuBar(ByVal MenuBar As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnMenuBar
    
        End Sub
    
        Public Sub OnQuit() Implements SHDocVw.DWebBrowserEvents2.OnQuit
    
        End Sub
    
        Public Sub OnStatusBar(ByVal StatusBar As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnStatusBar
    
        End Sub
    
        Public Sub OnTheaterMode(ByVal TheaterMode As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnTheaterMode
    
        End Sub
    
        Public Sub OnToolBar(ByVal ToolBar As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnToolBar
    
        End Sub
    
        Public Sub OnVisible(ByVal Visible As Boolean) Implements SHDocVw.DWebBrowserEvents2.OnVisible
    
        End Sub
    
        Public Sub PrintTemplateInstantiation(ByVal pDisp As Object) Implements SHDocVw.DWebBrowserEvents2.PrintTemplateInstantiation
    
        End Sub
    
        Public Sub PrintTemplateTeardown(ByVal pDisp As Object) Implements SHDocVw.DWebBrowserEvents2.PrintTemplateTeardown
    
        End Sub
    
        Public Sub PrivacyImpactedStateChange(ByVal bImpacted As Boolean) Implements SHDocVw.DWebBrowserEvents2.PrivacyImpactedStateChange
    
        End Sub
    
        Public Sub ProgressChange(ByVal Progress As Integer, ByVal ProgressMax As Integer) Implements SHDocVw.DWebBrowserEvents2.ProgressChange
    
        End Sub
    
        Public Sub PropertyChange(ByVal szProperty As String) Implements SHDocVw.DWebBrowserEvents2.PropertyChange
    
        End Sub
    
        Public Sub SetSecureLockIcon(ByVal SecureLockIcon As Integer) Implements SHDocVw.DWebBrowserEvents2.SetSecureLockIcon
    
        End Sub
    
        Public Sub StatusTextChange(ByVal Text As String) Implements SHDocVw.DWebBrowserEvents2.StatusTextChange
    
        End Sub
    
        Public Sub TitleChange(ByVal Text As String) Implements SHDocVw.DWebBrowserEvents2.TitleChange
    
        End Sub
    
        Public Sub UpdatePageStatus(ByVal pDisp As Object, ByRef nPage As Object, ByRef fDone As Object) Implements SHDocVw.DWebBrowserEvents2.UpdatePageStatus
    
        End Sub
    
        Public Sub WindowClosing(ByVal IsChildWindow As Boolean, ByRef Cancel As Boolean) Implements SHDocVw.DWebBrowserEvents2.WindowClosing
    
        End Sub
    
        Public Sub WindowSetHeight(ByVal Height As Integer) Implements SHDocVw.DWebBrowserEvents2.WindowSetHeight
    
        End Sub
    
        Public Sub WindowSetLeft(ByVal Left As Integer) Implements SHDocVw.DWebBrowserEvents2.WindowSetLeft
    
        End Sub
    
        Public Sub WindowSetResizable(ByVal Resizable As Boolean) Implements SHDocVw.DWebBrowserEvents2.WindowSetResizable
    
        End Sub
    
        Public Sub WindowSetTop(ByVal Top As Integer) Implements SHDocVw.DWebBrowserEvents2.WindowSetTop
    
        End Sub
    
        Public Sub WindowSetWidth(ByVal Width As Integer) Implements SHDocVw.DWebBrowserEvents2.WindowSetWidth
    
        End Sub
    End Class
    
    

額外的備忘稿

  • 如果您將自動化 Internet Explorer 這個處理程序都是一樣的。取代 AxWebBrowser1 Internet Explorer 您本機變數名稱。
  • 這個範例並不會考慮框架組。當您瀏覽到一個框架組時,您可能不在您的應用程式中會看到任何事件。在您的應用程式中必要時您必須加入程式碼來處理框架組的可能性。

参考

如 WebBrowser] 控制項,以及方法的相關資訊,請將屬性及事件 WebBrowser 控制項公開,參閱下列 MSDN 文件:
http://msdn2.microsoft.com/en-us/library/Aa752043.aspx (http://msdn2.microsoft.com/en-us/library/Aa752043.aspx)
如需有關 Microsoft Internet Explorer 開發 Web 為基礎的解決方案的詳細資訊,請造訪下列 Microsoft 網站:
http://msdn.microsoft.com/ie/ (http://msdn.microsoft.com/ie/)

http://support.microsoft.com/iep (http://support.microsoft.com/iep)

這篇文章中的資訊適用於:
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
關鍵字: 
kbmt kbhowtomaster kbwebbrowser KB311284 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:311284  (http://support.microsoft.com/kb/311284/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
共用
其他支援選項
Microsoft Community 支援論壇
直接與我們連絡
尋找 Microsoft 認證合作夥伴
Microsoft 市集