0

The goal is to indicate InputSimulator.SimulateMouseMoveEvent to user. So, i need to get coursor position on document on every move event, iam using EventListener with DOMEventType.OnMouseMove. But can`t get if DOMEventArgs contains MouseEvent properties?

Is it posible to do my way at all? Is there are any solutions to do this?

Vladyslav Tsvek
  • 244
  • 1
  • 10

1 Answers1

2

For the described purpose you need to subscribe to the mentioned event on the JavaScript side and uses the JS-.NET bridge.

This code sample demonstrates how to obtain the mouse position with the mousemove event: XAML

<Window x:Class="WpfApplication33.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpf="clr-namespace:DotNetBrowser.WPF;assembly=DotNetBrowser"
        xmlns:local="clr-namespace:WpfApplication33"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="900">
    <Grid Name="mainLayout">
        <Grid.RowDefinitions>
            <RowDefinition Height="24*"/>
            <RowDefinition Height="295*"/>
        </Grid.RowDefinitions>
        <Button x:Name="button" Content="Button" Grid.Row="0" Click="button_Click"/>
        <wpf:WPFBrowserView Name="browserView" Grid.Row="1"/>
    </Grid>
</Window>

C#

using System;
using System.Diagnostics;
using System.Windows;
using DotNetBrowser;
using DotNetBrowser.DOM;
using DotNetBrowser.DOM.Events;

namespace WpfApplication33
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            browserView.Browser.FinishLoadingFrameEvent += (s, e) =>
            {
                if(e.IsMainFrame)
                {
                    JSValue window = e.Browser.ExecuteJavaScriptAndReturnValue("window");
                    window.AsObject().SetProperty("CoordinatesObject", new CoordinatesObject());


                    //This code only notified that the onmousemove event was fired.
                    //It can't return the current mouse position.
                    //    DOMDocument document = browserView.Browser.GetDocument();
                    //    DOMElement documentElement = document.DocumentElement;
                    //    DOMEventHandler eventHandler = (sender, args) =>
                    //    {
                    //        Debug.WriteLine("OnMouseMove Fired");
                    //    };
                    //    documentElement.AddEventListener(DOMEventType.OnMouseMove, eventHandler, false);
                }
            };

            browserView.Browser.ScriptContextCreated += (sender, args) =>
            {
                args.Browser.ExecuteJavaScript(@"
                        document.onmousemove = getMouseXY;
                        function getMouseXY(e) {
                        CoordinatesObject.MousePosition(e.pageX, e.pageY);
                        }");
            };
            browserView.Browser.LoadURL("google.com");
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            browserView.InputSimulator.SimulateMouseMoveEvent(200, 150);
        }
    }

    internal class CoordinatesObject
    {
        public void MousePosition(int X, int Y)
        {
            Debug.WriteLine("X=" + X + " Y=" + Y);
        }
    }
}
Vladyslav Tsvek
  • 244
  • 1
  • 10