The easiest way to draw a sequence of points in WPF from code

I would like to create a WPF application that tracks the location of the mouse cursor by updating the image in the MouseMove event handler. My initial thought was to create a GeometryDrawing and then add paths to it, but I'm struggling to relate this to the code (although the Xaml for GeometryDrawings seems simple). What is the easiest way to connect this stuff - it's just for debugging, so I'm not interested in efficiency.

+5
source share
2 answers

How about using a polyline?

Here 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>

Here is the code behind:

private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
    polyline.Points.Add(e.GetPosition(canvas));
}
+13
source

. : EllipseGeometry , . - :

private GeometryGroup _allMousePoints = new GeometryGroup();

void OnMouseMove(...)
{
  _allMousePoints.Children.Add(
    new EllipseGeometry {
      Center = mouseLocation,
      RadiusX = 3,
      Radius Y = 3
    });
}

_allMousePoints .

+2

All Articles