目标是向用户指示InputSimulator.SimulateMouseMoveEvent
。所以,我需要在每次移动事件中获得courser在文档上的位置,我使用EventListener
和DOMEventType.OnMouseMove
。但是无法获取DOMEventArgs是否包含MouseEvent属性?
有可能按我的方式行事吗?有什么解决方案吗?
为了实现上述目的,您需要在JavaScript端订阅上述事件,并使用JS-.NET桥。
此代码示例演示如何使用mousemove事件获取鼠标位置: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);
}
}
}