この資料では、Visual Basic .NET で Web ブラウザ (WebBrowser) コントロールのドキュメント イベントを処理する方法を紹介します。
必要条件
推奨する必要なハードウェア、ソフトウェア、ネットワーク インフラストラクチャ、および Service Pack は、次のとおりです。
-
Microsoft Visual Studio .NET
-
Microsoft Internet Explorer 5.5 Service Pack 2 (SP2) 以降
この資料は、次のトピックについて詳しい知識のあるユーザーを対象としています。
-
Visual Studio .NET
-
Internet Explorer
-
Web ブラウザ (WebBrowser) コントロール
技法の説明
Web ブラウザ (WebBrowser) コントロールは、Visual Studio .NET または Microsoft Visual Studio の以前のバージョンで扱いやすいコントロールです。ただし、Visual Studio .NET では、Web ブラウザ コントロールのイベントを処理するのがより難しく感じられる場合があります。
以下のイベント インターフェイスでは、アプリケーション内で処理するドキュメント イベントの大部分が公開されています。
Mshtml.HTMLDocumentEvents2_Event
イベントを処理するためには、独自の Sub プロシージャまたは関数を作成して、イベントが発生したときに呼び出せるようにする必要があります。また、発生するイベントの署名を照合する必要があります。たとえば、以下の Sub プロシージャでは、ドキュメントの MouseOver イベントを処理します。
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_onmouseover Sub プロシージャは、ハンドラのアドレスを提供する第 2 パラメータの AddressOf ステートメントに渡されます。
プロジェクトの作成とコードの追加
以下のサンプルでは、Web ブラウザ コントロールによって
http://www.microsoft.com
(http://www.microsoft.com)
が参照されます。ページが読み込まれた後、OnMouseOver イベントおよび OnClick イベントがフックされます。以後、イベントが発生するたびに、リスト ボックスにテキストが追加されます。
-
Visual Studio .NET を起動します。
-
Visual Basic .NET で新しい Windows アプリケーション プロジェクトを作成します。
-
プロジェクト内に Microsoft.mshtml への参照を追加します。
-
ツールボックスで、[全般] をクリックします。
-
開いているパネルを右クリックし、[ツールボックスのカスタマイズ] をクリックします。
-
[Microsoft Web Browser] チェック ボックスをオンにして [OK] をクリックします。
-
ツールボックスで、[Explorer] をダブルクリックします。
-
ツールボックスで、[Windows フォーム] をクリックし、[ListBox] コントロールをダブルクリックします。
-
フォーム上で見やすいようにコントロールを配置します。
-
次のコードを AssemblyInfo.vb の先頭に追加し、既存の Import 文を置き換えます。
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
-
Form1.vb のコード ビューで、次のコードを "Windows フォーム デザイナで生成されたコード" と "End Class" の間に追加します。
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
-
次のコードを "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 のローカル変数名に置き換えてください。
-
このサンプルでは、フレームセットを考慮していません。フレームセットを参照した場合、アプリケーションにイベントが表示されない可能性があります。必要な場合は、アプリケーションにフレームセットを処理するコードを追加してください。
Web ブラウザ (WebBrowser) コントロールと、このコントロールが公開するメソッド、プロパティ、イベントの詳細については、以下の MSDN ドキュメントを参照してください。
Microsoft Internet Explorer 用の Web ベース ソリューションの開発の詳細については、以下のマイクロソフト Web サイトを参照してください。
文書番号: 311284 - 最終更新日: 2007年5月13日 - リビジョン: 3.1
この資料は以下の製品について記述したものです。
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Visual Basic .NET 2003 Standard Edition
| kbhowtomaster kbwebbrowser KB311284 |
"Microsoft Knowledge Baseに含まれている情報は、いかなる保証もない現状ベースで提供されるものです。Microsoft Corporation及びその関連会社は、市場性および特定の目的への適合性を含めて、明示的にも黙示的にも、一切の保証をいたしません。さらに、Microsoft Corporation及びその関連会社は、本文書に含まれている情報の使用及び使用結果につき、正確性、真実性等、いかなる表明・保証も行ないません。Microsoft Corporation、その関連会社及びこれらの権限ある代理人による口頭または書面による一切の情報提供またはアドバイスは、保証を意味するものではなく、かつ上記免責条項の範囲を狭めるものではありません。Microsoft Corporation、その関連会社 及びこれらの者の供給者は、直接的、間接的、偶発的、結果的損害、逸失利益、懲罰的損害、または特別損害を含む全ての損害に対して、状況のいかんを問わず一切責任を負いません。(Microsoft Corporation、その関連会社 またはこれらの者の供給者がかかる損害の発生可能性を了知している場合を含みます。) 結果的損害または偶発的損害に対する責任の免除または制限を認めていない地域においては、上記制限が適用されない場合があります。なお、本文書においては、文書の体裁上の都合により製品名の表記において商標登録表示、その他の商標表示を省略している場合がありますので、予めご了解ください。"