从代码在WPF中绘制点序列的最简单方法
问题描述:
我想创建一个WPF应用程序,该应用程序可以跟踪鼠标光标的位置,并在MouseMove事件处理程序中更新图像.我最初的想法是创建一个GeometryDrawing,然后为其添加路径,但是我在如何将其连接到代码中苦苦挣扎(尽管Xaml for GeometryDrawings看起来很简单).连接这些内容的最简单方法是-仅用于调试,因此我不关心效率.
I'd like to create a WPF app that traces the location of the mouse cursor, updating the image in the MouseMove event handler. My original thought was to create a GeometryDrawing and then add paths to that but I'm struggling with how to wire this up in code (though the Xaml for GeometryDrawings seems straightforward). What's the easiest way to wire this stuff up - its just for debugging so I'm not concerned about efficiency.
答
仅使用折线怎么样?
这是xaml:
<Window
x:Class="CursorLine.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
>
<Canvas x:Name="canvas" Background="#00FFFFFF" MouseMove="Canvas_MouseMove">
<Polyline x:Name="polyline" Stroke="DarkGreen" StrokeThickness="3"/>
</Canvas>
</Window>
这是背后的代码:
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
polyline.Points.Add(e.GetPosition(canvas));
}