2

I'm defining shapes which inherit from class Shape and implement the 'Geometry' property.

Here's an example:

public class Landmark : Shape
{
    public override bool IsInBounds(Point currentLocation)
    {

        return (((currentLocation.X >= Location.X - 3) && (currentLocation.X <= Location.X + 3)) && ((currentLocation.Y >= Location.Y - 3) && (currentLocation.Y <= Location.Y + 3)));
    }

    protected override Geometry DefiningGeometry
    {
        get
        {
            var landMark = new EllipseGeometry {Center = Location};

            Stroke = Brushes.Red;
            return landMark;
        }
    }

    protected override void OnIsMouseDirectlyOverChanged(DependencyPropertyChangedEventArgs e)
    {
        StrokeThickness = IsMouseDirectlyOver ? 12 : 6;
        Mouse.OverrideCursor = IsMouseDirectlyOver ? Mouse.OverrideCursor = Cursors.ScrollAll : Mouse.OverrideCursor = Cursors.Arrow;
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Location = e.GetPosition(this);
            InvalidateVisual();
        }
    }
}

When I click on the Shape and move my mouse, I expect the Shape to be redrawn in the new location - and it does work.

However, if I move the mouse "too" quickly, then I'm "leaving" the OnMouseMove event, and the shape gets stuck in the last position which the mouse pointer and the Shape's location were in "sync".

Can such issue be solved?

Idanis
  • 1,918
  • 6
  • 38
  • 69

1 Answers1

3

Yes, by capturing the Mouse.

For that to work you must establish when to capture and when to release. Since you want this to work with the left mouse button, you can capture in OnMouseDown and release the mouse in OnMouseUp.

Community
  • 1
  • 1
Markus Hütter
  • 7,796
  • 1
  • 36
  • 63