- 題名: IEでクリックしたアイテムを取得する方法
- 日時: 2005/03/09 23:47:08
- ID: 9706
- この記事の返信元:
- (なし)
- この記事への返信:
- [9711] Re[1]: IEでクリックしたアイテムを取得する方法2005/03/10 10:56:32
- ツリーを表示
InternetExplorerオブジェクトで試したことはありませんが、
WebBrowserであれば、こんな感じで取得できます。
'イベント取得用のクラスを用意しておく。
Public Class DHTMLEventHandler
Implements IDisposable
Private OwnerDocument As mshtml.HTMLDocument
Public Event OnEvent(ByVal E As mshtml.IHTMLEventObj)
Public Sub New(ByVal OwnerDocument As mshtml.HTMLDocument)
Me.OwnerDocument = OwnerDocument
End Sub
<System.Runtime.InteropServices.Dispid(0)> Public Sub DHTMLEvent()
Dim W As mshtml.IHTMLWindow2 = Me.OwnerDocument.parentWindow
Dim E As mshtml.IHTMLEventObj = W.event
RaiseEvent OnEvent(E)
System.Runtime.InteropServices.Marshal.ReleaseComObject(E)
System.Runtime.InteropServices.Marshal.ReleaseComObject(W)
End Sub
Public Sub Dispose() Implements System.IDisposable.Dispose
If Not IsNothing(OwnerDocument) Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(Me.OwnerDocument)
End If
Me.OwnerDocument = Nothing
End Sub
End Class
Private WithEvents OnClickHandler As DHTMLEventHandler
Private Doc As mshtml.HTMLDocument
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.AxWebBrowser1.Navigate2("http://www.google.co.jp/")
End Sub
Private Sub OnClickHandler_OnEvent(ByVal E As mshtml.IHTMLEventObj _
) Handles OnClickHandler.OnEvent
Dim O As mshtml.IHTMLElement = E.srcElement
'クリックされた要素を画面に表示
ListBox1.Items.Add(String.Format("<{0}> ({1}, {2})", O.tagName, E.x, E.y))
Trace.WriteLine(O.outerHTML)
System.Runtime.InteropServices.Marshal.ReleaseComObject(O)
End Sub
'ページが読み込まれる度に、onclickイベントを割り当てる。
Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, _
ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent _
) Handles AxWebBrowser1.DocumentComplete
If Not IsNothing(OnClickHandler) Then
OnClickHandler.Dispose()
System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc)
End If
Dim D As Object = Me.AxWebBrowser1.Document
If TypeOf D Is mshtml.HTMLDocument Then
Doc = DirectCast(D, mshtml.HTMLDocument)
OnClickHandler = New DHTMLEventHandler(Doc)
Dim Body As mshtml.IHTMLElement = Doc.body
Body.onclick = OnClickHandler
System.Runtime.InteropServices.Marshal.ReleaseComObject(Body)
Else
System.Runtime.InteropServices.Marshal.ReleaseComObject(D)
End If
End Sub
分類:[.NET]
こんばんは。
サイトのページをインターネットエクスプローラに表示し、ページ中にあるアイテムをクリックした時に、プログラムにてそのアイテムの情報、フレームの番号、タグは何か(tags(?))、アイテムの番号(item(n))って取得する事ができるでしょうか?どのような手法がありますか?
また、アイテムをクリックしたらその箇所のHTMLの内容が取得できる手法でもよいのですが。よろしければ教えてください。よろしくお願い致します。