Scottplot获取鼠标滑过的位置
注册鼠标移动事件
MouseMove += DisplayScaling_MouseMove;
移动执行代码
Point p = e.GetPosition(WpfPlot1);
ScottPlot.Pixel mousePixel = new(p.X * WpfPlot1.DisplayScale, p.Y * WpfPlot1.DisplayScale);
ScottPlot.Coordinates coordinates = WpfPlot1.Plot.GetCoordinates(mousePixel);
Crosshair.Position = coordinates;
WpfPlot1.Refresh();
完整CS代码
using System.Windows;
namespace WPF_Demo.DemoWindows;
public partial class DisplayScaling : Window, IDemoWindow
{
public string DemoTitle => "Display Scaling";
public string Description => "Demonstrates how to track " +
"mouse position on displays which use DPI scaling.";
readonly ScottPlot.Plottables.Crosshair Crosshair;
public DisplayScaling()
{
InitializeComponent();
WpfPlot1.Plot.Add.Signal(ScottPlot.Generate.Sin());
WpfPlot1.Plot.Add.Signal(ScottPlot.Generate.Cos());
Crosshair = WpfPlot1.Plot.Add.Crosshair(0, 0);
MouseMove += DisplayScaling_MouseMove;
}
private void DisplayScaling_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
Point p = e.GetPosition(WpfPlot1);
ScottPlot.Pixel mousePixel = new(p.X * WpfPlot1.DisplayScale, p.Y * WpfPlot1.DisplayScale);
ScottPlot.Coordinates coordinates = WpfPlot1.Plot.GetCoordinates(mousePixel);
Crosshair.Position = coordinates;
WpfPlot1.Refresh();
}
private void WpfPlot1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
}
}
完整xam
<Window x:Class="WPF_Demo.DemoWindows.DisplayScaling"
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:local="clr-namespace:WPF_Demo.DemoWindows" xmlns:wpf="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="Display Scaling Demo" Height="400" Width="600">
<Viewbox >
<wpf:WpfPlot Name="WpfPlot1" Width="500" Height="400" Mouse.MouseMove="WpfPlot1_MouseMove"/>
</Viewbox>
</Window>
运行效果