マウスオーバーをしている時間の計測 への返答
投稿で使用できる特殊コードの説明。(別タブで開きます。)
以下の返答は逆順(新しい順)に並んでいます。
投稿者 そそそ  (学生)
投稿日時
2011/6/30 19:33:32
webbrowserコントロールで表示したページでマウスオーバーするとhtmlの要素をtextboxに表示するプログラムを作っています。
ある要素にどのくらいの時間マウスが停滞していたかを測りたいのですがどうすればよいでしょうか?
現在のプログラムは以下のようになっています。
Imports System.Runtime.InteropServices.Marshal
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://www.google.co.jp/")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
'WebBrowser1.Document.AttachEventHandler("onmouseup", AddressOf document_onmouseup)
WebBrowser1.Document.AttachEventHandler("onmouseover", AddressOf document_onmouseover)
End Sub
Sub ReleaseObject(ByVal o As Object)
If o IsNot Nothing AndAlso IsComObject(o) Then
ReleaseComObject(o)
End If
End Sub
Sub document_onmouseover(ByVal sender As Object, ByVal e As EventArgs)
TextBox1.Text = WebBrowser1.Document.GetElementFromPoint(WebBrowser1.PointToClient(Control.MousePosition)).OuterText
TextBox2.Text = WebBrowser1.Document.GetElementFromPoint(WebBrowser1.PointToClient(Control.MousePosition)).OuterHtml
End Sub
End Class
ある要素にどのくらいの時間マウスが停滞していたかを測りたいのですがどうすればよいでしょうか?
現在のプログラムは以下のようになっています。
Imports System.Runtime.InteropServices.Marshal
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://www.google.co.jp/")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
'WebBrowser1.Document.AttachEventHandler("onmouseup", AddressOf document_onmouseup)
WebBrowser1.Document.AttachEventHandler("onmouseover", AddressOf document_onmouseover)
End Sub
Sub ReleaseObject(ByVal o As Object)
If o IsNot Nothing AndAlso IsComObject(o) Then
ReleaseComObject(o)
End If
End Sub
Sub document_onmouseover(ByVal sender As Object, ByVal e As EventArgs)
TextBox1.Text = WebBrowser1.Document.GetElementFromPoint(WebBrowser1.PointToClient(Control.MousePosition)).OuterText
TextBox2.Text = WebBrowser1.Document.GetElementFromPoint(WebBrowser1.PointToClient(Control.MousePosition)).OuterHtml
End Sub
End Class
まだ不完全ですがコードを載せておきます。
Imports System.Runtime.InteropServices.Marshal
Public Class Form1
Private watch As New Stopwatch()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://www.google.co.jp/")
Timer1.Interval = 20
Timer1.Enabled = True
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
'WebBrowser1.Document.AttachEventHandler("onmouseup", AddressOf document_onmouseup)
WebBrowser1.Document.AttachEventHandler("onmouseover", AddressOf document_onmouseover)
WebBrowser1.Document.AttachEventHandler("onmouseout", AddressOf document_onmouseout)
End Sub
Sub document_onmouseover(ByVal sender As Object, ByVal e As EventArgs)
TextBox1.Text = WebBrowser1.Document.GetElementFromPoint(WebBrowser1.PointToClient(Control.MousePosition)).OuterText
TextBox2.Text = WebBrowser1.Document.GetElementFromPoint(WebBrowser1.PointToClient(Control.MousePosition)).OuterHtml
TextBox3.Text = Control.MousePosition.ToString
watch.Start()
End Sub
Sub document_onmouseout(ByVal sender As Object, ByVal e As EventArgs)
RichTextBox1.Text = WebBrowser1.Document.GetElementFromPoint(WebBrowser1.PointToClient(Control.MousePosition)).OuterHtml + watch.Elapsed.ToString + Chr(13) & Chr(10) + RichTextBox1.Text
watch.Stop()
watch.Reset()
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Label1.Text = watch.Elapsed.ToString()
End Sub
End Class