There is a known issue related using WPFBrowserView (heavyweight rendering mode) with mouse events. The mouse events do not return the correct mouse position with GetPosition() method. It returns the top left point of the native window.


Cause:

The issue is related to the specific integration of mouse events with WPFBrowserView.

The issue is not reproducible with lightweight rendering mode.


Solution:

Since version 1.18, DotNetBrowser provides classes (WPFMouseEventArgs for MouseEventArgs and WPFMouseButtonEventArgs for MouseButtonEventArgs) which allow getting correct coordinates of the mouse position.


The following sample code demonstrates how to obtain the correct mouse position:

C#

using System.Diagnostics;
using System.Windows;
using DotNetBrowser.WPF;

namespace WPFMouseEvents
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            WPFBrowserView browserView = new WPFBrowserView();
            mainLayout.Children.Add(browserView);

            browserView.MouseDown += BrowserView_MouseDown;
            browserView.MouseMove += BrowserView_MouseMove;

            browserView.Browser.LoadURL("teamdev.com");
        }

        private void BrowserView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            WPFMouseEventArgs mouseEventArgs = e as WPFMouseEventArgs;
            Point pt = mouseEventArgs.GetPosition(this);
            Debug.WriteLine("Mouse Move: " + pt);
        }

        private void BrowserView_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            WPFMouseButtonEventArgs mouseButtonEventArgs = e as WPFMouseButtonEventArgs;
            Point pt = mouseButtonEventArgs.GetPosition(this);
            Debug.WriteLine("Mouse Down: " + pt);
        }
    }
}

VB.NET

Imports DotNetBrowser.WPF

Namespace WPFMouseEvents
    Public Partial Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
            Dim browserView As WPFBrowserView = New WPFBrowserView()
            mainLayout.Children.Add(browserView)
            AddHandler browserView.MouseDown, AddressOf BrowserView_MouseDown
            AddHandler browserView.MouseMove, AddressOf BrowserView_MouseMove
            browserView.Browser.LoadURL("teamdev.com")
        End Sub

        Private Sub BrowserView_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
            Dim mouseEventArgs As WPFMouseEventArgs = TryCast(e, WPFMouseEventArgs)
            Dim pt As Point = mouseEventArgs.GetPosition(Me)
            Debug.WriteLine("Mouse Move: " & pt.ToString())
        End Sub

        Private Sub BrowserView_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
            Dim mouseButtonEventArgs As WPFMouseButtonEventArgs = TryCast(e, WPFMouseButtonEventArgs)
            Dim pt As Point = mouseButtonEventArgs.GetPosition(Me)
            Debug.WriteLine("Mouse Down: " & pt.ToString())
        End Sub
    End Class
End Namespace