DOBON.NET DOBON.NETプログラミング掲示板過去ログ

WebBrowserコントロールでマウスイベントの取得

分類:[.NET]

VB2005で、WebBrowserコントロールのマウスのクリックやMouseMoveなどのイベントの取得は通常できないため、WebBrowserから派生クラスを作成し、そのイベントを取得したいのですが、WndProcをオーバーライドしてDebug.Print(m.ToString)で確認したところ、MOUSEDOWNは取得できるのですが、MOUSEUPやMOUSEMOVEが取得できずにいます。どうすればMOUSEUPやMOUSEMOVEを取得できるのかわかる方、ぜひ教えてください。よろしくお願いします。
かふぇおれさん,こんばんは。

DocumentCompletedイベント以後に,Bodyエレメントのイベントを横取りして
新たにイベントを発生させればそれっぽくなるのではないでしょうか。
VB2008EEで作成したサンプルコードです。試してみてください。

Public Class Form1

    Private WithEvents WebBrowser As New ExWebBrowser

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WebBrowser.Dock = DockStyle.Fill
        Me.Controls.Add(WebBrowser)
        Me.WebBrowser.GoHome()
    End Sub

    Private Sub WebBrowser_MouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) Handles WebBrowser.MouseDown
        Debug.Print("MouseDown!")
    End Sub

    Private Sub WebBrowser_MouseEnter(ByVal e As System.Windows.Forms.MouseEventArgs) Handles WebBrowser.MouseEnter
        Debug.Print("MouseEnter!")
    End Sub

    Private Sub WebBrowser_MouseLeave(ByVal e As System.Windows.Forms.MouseEventArgs) Handles WebBrowser.MouseLeave
        Debug.Print("MouseLeave!")
    End Sub

    Private Sub WebBrowser_MouseMove(ByVal e As System.Windows.Forms.MouseEventArgs) Handles WebBrowser.MouseMove
        Debug.Print("MouseMove!")
    End Sub

    Private Sub WebBrowser_MouseOver(ByVal e As System.Windows.Forms.MouseEventArgs) Handles WebBrowser.MouseOver
        Debug.Print("MouseOver!")
    End Sub

    Private Sub WebBrowser_MouseUp(ByVal e As System.Windows.Forms.MouseEventArgs) Handles WebBrowser.MouseUp
        Debug.Print("MouseUp!")
    End Sub

End Class

Public Class ExWebBrowser
    Inherits WebBrowser

    Private WithEvents Body As HtmlElement

    Public Shadows Event MouseDown(ByVal e As MouseEventArgs)
    Public Shadows Event MouseUp(ByVal e As MouseEventArgs)
    Public Shadows Event MouseEnter(ByVal e As MouseEventArgs)
    Public Shadows Event MouseLeave(ByVal e As MouseEventArgs)
    Public Shadows Event MouseMove(ByVal e As MouseEventArgs)
    Public Shadows Event MouseOver(ByVal e As MouseEventArgs)

    Protected Overrides Sub OnDocumentCompleted(ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        Me.Body = Me.Document.Body
        MyBase.OnDocumentCompleted(e)
    End Sub

    Private Sub Body_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles Body.MouseDown
        RaiseEvent MouseDown(New MouseEventArgs(e.MouseButtonsPressed, 1, e.ClientMousePosition.X, e.ClientMousePosition.Y, 0))
    End Sub

    Private Sub Body_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles Body.MouseUp
        RaiseEvent MouseUp(New MouseEventArgs(e.MouseButtonsPressed, 1, e.ClientMousePosition.X, e.ClientMousePosition.Y, 0))
    End Sub

    Private Sub Body_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles Body.MouseEnter
        RaiseEvent MouseEnter(New MouseEventArgs(Windows.Forms.MouseButtons.None, 0, e.ClientMousePosition.X, e.ClientMousePosition.Y, 0))
    End Sub

    Private Sub Body_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles Body.MouseLeave
        RaiseEvent MouseLeave(New MouseEventArgs(Windows.Forms.MouseButtons.None, 0, e.ClientMousePosition.X, e.ClientMousePosition.Y, 0))
    End Sub

    Private Sub Body_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles Body.MouseMove
        RaiseEvent MouseMove(New MouseEventArgs(Windows.Forms.MouseButtons.None, 0, e.ClientMousePosition.X, e.ClientMousePosition.Y, 0))
    End Sub

    Private Sub Body_MouseOver(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles Body.MouseOver
        RaiseEvent MouseOver(New MouseEventArgs(Windows.Forms.MouseButtons.None, 0, e.ClientMousePosition.X, e.ClientMousePosition.Y, 0))
    End Sub

End Class

DOBON.NET | プログラミング道 | プログラミング掲示板