如何处理在 Visual Basic.NET 应用程序中的文档事件

文章翻译 文章翻译
文章编号: 311284 - 查看本文应用于的产品
展开全部 | 关闭全部

本文内容

概要

本文介绍了如何在 Visual Basic.NET 中捕获文档的 web 浏览器控件的事件。

要求

下面的列表列出了推荐使用的硬件、 软件、 网络基础结构和所需的服务包:
  • Microsoft Visual Studio.NET
  • Microsoft Internet Explorer 5.5 Service Pack 2 (SP2) 或更高版本
本文假定您熟悉下列主题:
  • Visual Studio.NET
  • Internet Explorer
  • Web 浏览器控件

方法说明

Web 浏览器控件是容易控制,若要在 Visual Studio.NET 或 Microsoft Visual Studio 的早期版本中使用。但是,您可能会发现它更难以处理的 Visual Studio.NET 中的 web 浏览器控件的事件。

下面的事件接口公开大部分应用程序中处理的文档事件:
Mshtml.HTMLDocumentEvents2_Event
				
若要处理该事件,必须创建您自己的Sub过程或事件发生时,您可以调用的函数。您必须将触发该事件的签名匹配。例如,下面的Sub过程处理鼠标悬停事件的文档:
Private Sub document_onmouseover(ByVal e As mshtml.IHTMLEventObj)
				
该事件处理程序的位置后,您必须挂钩事件。您可以在 web 浏览器控件激发挂钩事件DocumentComplete事件后的任何时间。下面是挂接的语法:
AddHandler CType(document, _
mshtml.HTMLDocumentEvents2_Event).onmouseover, _
AddressOf Me.document_onmouseover
				
此代码调用AddHandler语句,并使用CType函数将事件传递。CType函数将强制转换为适当的类型的文档对象 (mshtml。HTMLDocumentEvents2_Event),并随后将OnMouseOver事件传递的。Me.document_onmouseoverSub过程被传递给第二个参数, AddressOf语句,它提供了处理程序的地址。

创建项目并添加代码

在下面的示例中,浏览到 web 浏览器控件 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 窗体,然后双击ListBox控件。
  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. Form1.vbPublic 的代码视图中添加Windows 窗体设计器生成的代码最终的类之间,下面的代码
    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. 端的类添加以下代码
    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,此过程都是相同的。替换您的 Internet Explorer 的本地变量名称为AxWebBrowser1
  • 此示例不考虑框架集。当您导航到一个框架集时,您可能无法看到您的应用程序中的任何事件。如果需要在您的应用程序,您必须添加代码以处理的框架集的可能性。

参考

有关 web 浏览器控件中,以及这些方法的详细信息,以及 web 浏览器控件公开的事件是指下面的 MSDN 文档:
http://msdn2.microsoft.com/en-us/library/Aa752043.aspx
有关为 Microsoft Internet Explorer 开发基于 Web 的解决方案的详细信息,请访问下面的 Microsoft 网站:
http://msdn.microsoft.com/ie/

http://support.microsoft.com/iep

属性

文章编号: 311284 - 最后修改: 2013年11月23日 - 修订: 4.0
这篇文章中的信息适用于:
  • Microsoft Visual .NET 2002 标准版
  • Microsoft Visual Basic .NET 2003 标准版
关键字:?
kbhowtomaster kbwebbrowser kbmt KB311284 KbMtzh
机器翻译
重要说明:本文是由 Microsoft 机器翻译软件进行的翻译并可能由 Microsoft 社区通过社区翻译机构(CTF)技术进行后期编辑,或可能是由人工进行的翻译。Microsoft 同时向您提供机器翻译、人工翻译及社区后期编辑的文章,以便对我们知识库中的所有文章以多种语言提供访问。翻译的文章可能存在词汇、句法和/或语法方面的错误。Microsoft 对由于内容的误译或客户对内容的使用所导致的任何不准确、错误或损失不承担责任。
点击这里察看该文章的英文版: 311284
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。

提供反馈

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com